Moderadores: andre_luis, 51, guest2003, Renie
module H1_spi
CONST MASTER_OSC_DIV4 = 0 ' Master clock=Fosc/4
CONST MASTER_OSC_DIV16 = 1 ' Master clock=Fosc/16
CONST MASTER_OSC_DIV64 = 2 ' Master clock=Fosc/64
CONST MASTER_TMR2 = 3 ' Master clock source TMR2
CONST SLAVE_SS_ENABLE = 4 ' Master Slave select enabled
CONST SLAVE_SS_DIS = 5 ' Master Slave select disabled
CONST DATA_SAMPLE_MIDDLE = 0 ' Input data sampled in middle of interval
CONST DATA_SAMPLE_END = 128 ' Input data sampled at the end of interval
CONST CLK_IDLE_HIGH = 16 ' Clock idle high
CONST CLK_IDLE_LOW = 0 ' Clock idle low
CONST LOW_2_HIGH = 1 ' Data transmit on low to high edge
CONST HIGH_2_LOW = 0 ' Data transmit on high to low edge
implements
sub procedure H1_SPI_INIT_ADVANCED(dim master, data_sample, clock_idle, transmit_edge as byte)
TRISC.5 = 0 ' SDO is output
TRISC.3 = 0 ' SCK is output
TRISC.4 = 1 ' SDI is input
SSP1CON1 = 0 ' clear MSSP control register
SSP1CON1 = SSP1CON1 Or master ' master clock speed and master/slave modes
SSP1CON1 = SSP1CON1 Or clock_idle ' clock idle high or low
SSP1STAT = SSP1STAT Or data_sample ' data sampling in middle or end
If clock_idle <> 0 Then ' if clock idle is high (CPE=1) then
If transmit_edge = 0 Then ' if transmit edge is high_2_low then
SSP1STAT.6 = 1 ' (CKE=1) transmit occurs on transition from active to idle clock state
End If
Else ' if clock idle is low (CPE=0) then
If transmit_edge = 1 Then ' if tranmit edge is low_2_high then
SSP1STAT.6 = 1 ' (CKE=1) transmit occurs on transition from active to idle clock state
End If
End If
SSP1CON1.5 = 1 ' enables serial port and configures SCK, SDO, SDI as serial port pins
end sub
sub procedure H1_SPI_INIT
TRISC.5 = 0 ' SDO is output
TRISC.3 = 0 ' SCK is output
TRISC.4 = 1 ' SDI is input
SSP1CON1 = 0 ' clear MSSP control register
SSP1STAT = 64 ' transmit occurs on transition from active to idle clock state
SSP1CON1.5 = 1 ' enables serial port and configures SCK, SDO, SDI as serial port pins
end sub
sub procedure H1_SPI_WRITE(dim data as byte)
dim temp as byte volatile
SSP1BUF = data ' load data into SPI output buffer and send
While (SSP1STAT And 1) = 0 ' wait until 'Buffer-Full' flag is set
nop
Wend
temp = SSP1BUF ' dummy read receive buffer to clear BF flag
end sub
sub function H1_SPI_READ(dim buffer as byte) as byte
SSP1BUF = buffer ' load data into SPI output buffer and send
While (SSP1STAT And 1) = 0 ' wait until 'Buffer-Full' flag is set
nop
Wend
result = SSP1BUF ' read receive buffer and clear BF flag
end sub
end.
module usart_c67
implements
sub procedure USART_INIT
' compiler manages initial calculation of baud speed
' this is the setup for 9600 baud, Fosc=8MHz, which the compiler
' puts in the main program before the call to USART_INIT()
' SPBRG = 51
' TXSTA.BRGH = 1
TXSTA.5 = 1 ' transmit enable bit
RCSTA = 144 ' serial port enabled; continuous receive enabled
TRISC.7 = 1 ' RX pin is input
TRISC.6 = 0 ' TX pin is output
While Testbit(PIR1, 5) = 1 ' test RCIF...will hang here if set...no way to clear RCIF
Wend ' unless RX register is read
end sub
sub function USART_DATA_READY as byte
result = testbit(PIR1, 5) ' 0= RX register is empty; 1= byte in RX register
end sub
sub procedure USART_WRITE(dim data as byte)
While Testbit(TXSTA, 1) = 0 ' wait here until TX register is empty
nop
Wend
TXREG = data ' load TX register with new value and transmit
end sub
sub function USART_READ as byte
result = (RCSTA and %00000110)
If result <> 0 Then ' se ocorreu overrun ou framming erro
RCSTA.4 = 0 ' para recepcao continua
RCSTA.4 = 1 ' inicializa a recepcao continua
ax = 1
else
result = RCREG ' read value off top of RX register stack
End If ' overrun error is now cleared
end sub
sub procedure USART_WRITE_TEXT(dim byref uart_text as string[20])
dim i, data as byte
i = 0
data = uart_text[0] ' get first value
While data <> 0 ' look for end of string character ('0')
USART_WRITE(data) ' send character
inc(i)
data = uart_text[i] ' get next character
Wend ' loop to send new character
end sub
sub procedure USART_READ_TEXT(dim byref output, delimiter as string[20])
dim i, j, data, data2, lasti as byte
i = 0
j = 0
lasti = 0
data2 = delimiter[0]
While true
If Usart_Data_Ready <> 0 Then ' something has arrived
data = USART_Read ' read char
output[i] = data ' store char in data (delimiter is stored too)
If data = data2 Then ' does it match the sequence?
lasti = i - j ' indicate where output should end
inc(j) ' next letter in sequence
Else
j = 0 ' sequences do not match, reset counters
lasti = 0
End If
data2 = delimiter[j] ' get next delimiter
If data2 = 0 Then ' stop if the end of delimiter is reached
break
End If
inc(i)
If i > 20 Then ' too many chars received
lasti = 19
break
End If
End If
Wend
output[lasti] = 0 ' place the end of string before delimiter
end sub
end.
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante