adwnis123
Full Member level 4
- Joined
- Jun 19, 2014
- Messages
- 214
- Helped
- 0
- Reputation
- 0
- Reaction score
- 1
- Trophy points
- 18
- Activity points
- 1,737
Hello, I have a PIC18F452 that is receiving data through SPI.The PIC is connected to an LCD16x2 where I want to display data. The data I send is "Hello, world!", however it does not show it correctly, it shows irrelevant characters! Can you help me ? Here is my code:
Code:
// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
void main()
{
char cmd,dummy;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
TRISA.F5 = 1; // SS (INPUT)
//PORTA.F5 = 0;
TRISC.F3 = 1; // SCLK (INPUT)
//PORTC.F3 = 0;
TRISC.F4 = 1; // DATA IN (INPUT)
//PORTC.F4 = 0;
TRISC.F5 = 0; // DATA OUT (OUTPUT)
//PORTC.F5 = 0;
/*
bit 7 SMP: Sample bit
SPI Master mode:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
SPI Slave mode:
SMP must be cleared when SPI is used in Slave mode
*/
SSPSTAT.SMP=0;
/*
bit 6 CKE: SPI Clock Edge Select
When CKP = 0:
1 = Data transmitted on rising edge of SCK
0 = Data transmitted on falling edge of SCK
When CKP = 1:
1 = Data transmitted on falling edge of SCK
0 = Data transmitted on rising edge of SCK
*/
SSPSTAT.CKE=0;
/*
bit 7 WCOL: Write Collision Detect bit (Transmit mode only)
1 = The SSPBUF register is written while it is still transmitting the previous word
(must be cleared in software)
0 = No collision
*/
SSPCON1.WCOL=0; //bit 7
/*
bit 6 SSPOV: Receive Overflow Indicator bit
SPI Slave mode:
1 = A new byte is received while the SSPBUF register is still holding the previous data.
In case
of overflow, the data in SSPSR is lost. Overflow can only occur in Slave mode.The user
must read the SSPBUF, even if only transmitting data, to avoid setting overflow
(must be cleared in software).
0 = No overflow
Note: In Master mode, the overflow bit is not set since each new reception (and
transmission) is initiated by writing to the SSPBUF register.
*/
// SSPCON1.SSPOV=0; //bit 6
/*
bit 5 SSPEN: Synchronous Serial Port Enable bit
1 = Enables serial port and configures SCK, SDO, SDI, and SS as serial port pins
0 = Disables serial port and configures these pins as I/O port pins
Note: When enabled, these pins must be properly configured as input or output.
*/
SSPCON1.SSPEN=1; //bit 5
/*
bit 4 CKP: Clock Polarity Select bit
1 = IDLE state for clock is a high level
0 = IDLE state for clock is a low level
*/
SSPCON1.CKP=1;
/*
bit 3-0 SSPM3:SSPM0: Synchronous Serial Port Mode Select bits
0101 = SPI Slave mode, clock = SCK pin, SS pin control disabled, SS can be used as I/O pin
0100 = SPI Slave mode, clock = SCK pin, SS pin control enabled
0011 = SPI Master mode, clock = TMR2 output/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4
*/
SSPCON1.SSPM3=0; //bit 3
SSPCON1.SSPM2=1; //bit 2
SSPCON1.SSPM1=0; //bit 1
SSPCON1.SSPM0=0; //bit 0
// SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE,_SPI_DATA_SAMPLE_MIDDLE,
// _SPI_CLK_IDLE_HIGH,_SPI_HIGH_2_LOW);
Delay_ms(200);
while(1)
{
while(SSPSTAT.BF==1)
{
cmd=SPI1_Read(SSPBUF);
Lcd_Out(1,1,cmd);
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
}
SSPCON1.SSPOV=0;
}
}