i get only 2q only and then a OERR but the code can't continue and means i just got 2 input of data only but i need to get it like in the picture
**broken link removed**
**broken link removed**
Code:
#include <pic18f2620.h>
#define LCD_data PORTB
#define LCD_D7 PORTBbits.RB1
#define LCD_rs PORTAbits.RA0
#define LCD_rw PORTAbits.RA1
#define LCD_en PORTAbits.RA2
void LCD_busy(void);
void LCD_init(void);
void LCD_command(unsigned char var);
void LCD_senddata(unsigned char var);
void LCD_sendstring(unsigned char *var);
void camera_tx (unsigned char);
void camera_cmd (unsigned char , unsigned char , unsigned char, unsigned char , unsigned char , unsigned char );
void camera_rx (void);
unsigned char rx_buffer[12];
void main(void)
{
int x,y;
ADCON1 = 0x0F;
TRISA = 0x00;
TRISB = 0x00;
RCSTAbits.SPEN = 1;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
TXSTAbits.TXEN = 1;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TX9 = 0;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 1;
BAUDCONbits.BRG16 = 0;
SPBRG = 21;
LCD_init();
LCD_sendstring("camera sync");
LCD_command(0x01); //LCD clear screen before printing new word
for(x=0;x<=60;x++) //max 60 times to send sync command to establish camera connection
{
LCD_sendstring("a");
//LCD_command(0x01); //LCD clear screen before printing new word
camera_cmd(0xAA , 0x0D , 0x00 , 0x00 , 0x00 , 0x00); //sync command
LCD_busy();
if(PIR1bits.RCIF==1)
{
LCD_command(0x01);
LCD_sendstring("ack coming");
LCD_command(0x01); //LCD clear screen before printing new word
rx_buffer[0]=RCREG;
for(y=1;y<12;y++)
{
// while(!PIR1bits.RCIF);
LCD_sendstring("q");
while(!PIR1bits.RCIF)
{
if(RCSTAbits.OERR ==1)
{
LCD_sendstring("OERR");
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
}
rx_buffer[y]=RCREG;
}
if(rx_buffer[0]== 0xAA && rx_buffer[1]== 0x0E /*&& rx_buffer[6]==0xAA && rx_buffer[7] == 0x0D && rx_buffer[2] == 0x0D*/) //ACK
{
LCD_command(0x01);
LCD_sendstring("com ok");
break;
}
}
if(rx_buffer[0]== 0xAA && rx_buffer[1]== 0x0E && rx_buffer[6]==0xAA /*&& rx_buffer[7] == 0x0D && rx_buffer[2] == 0x0D*/) //ACK
{
LCD_command(0x01);
LCD_sendstring("com2 ok");
break;
}
}
if(x==60)
{
LCD_command(0x01);
LCD_sendstring("fail");
}
LCD_busy();
LCD_sendstring("sync bac");
if(RCSTAbits.OERR ==1)
{
LCD_sendstring("OERR");
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
/* if(PIR1bits.RCIF==1)
{
LCD_sendstring("sync coming");
LCD_command(0x01); //LCD clear screen before printing new word
rx_buffer[0]=RCREG;
for(y=1;y<6;y++)
{
LCD_sendstring("w");
rx_buffer[y]=RCREG;
}
}
if(rx_buffer[6]!= 0xAA && rx_buffer[7]!= 0x0D && rx_buffer[8] != 0x00)
{
LCD_command(0x01);
LCD_sendstring("fail2");
// while(1);
}
else if(rx_buffer[6]== 0xAA && rx_buffer[7]== 0x0D && rx_buffer[8] == 0x00)
{
camera_cmd(0xAA , 0x0E , 0x0D , 0x00 , 0x00 , 0x00); //ACK command to finalize connection
LCD_command(0x01);
LCD_sendstring("sync complete");
}*/
}
void LCD_init()
{
LCD_data = 0x38; //Function set: 2 Line, 8-bit, 5x7 dots
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x0F; //Display on, Curson blinking command
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x01; //Clear LCD
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
LCD_data = 0x06; //Entry mode, auto increment with no shift
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy();
LCD_data = 0x80; //cursor position at first row
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy();
}
void LCD_busy()
{
int i,j;
for(i=0;i<50;i++)
for(j=0;j<255;j++);
}
void LCD_command(unsigned char var)
{
LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in instruction register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
}
void LCD_senddata(unsigned char var)
{
LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots
LCD_rs = 1; //Selected data register
LCD_rw = 0; //We are writing
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
}
void LCD_sendstring(unsigned char *var)
{
while(*var) //till string ends
LCD_senddata(*var++); //send characters one by one
}
void camera_tx (unsigned char var)
{
while(!PIR1bits.TXIF); //check TXREG full, load TXREG if not full
TXREG = var;
}
// camera_command
void camera_cmd (unsigned char cmd1 , unsigned char cmd2 , unsigned char cmd3 , unsigned char cmd4 , unsigned char cmd5 , unsigned char cmd6)
{
camera_tx(cmd1);
camera_tx(cmd2);
camera_tx(cmd3);
camera_tx(cmd4);
camera_tx(cmd5);
camera_tx(cmd6);
while(!TXSTAbits.TRMT); //check until data finish send out from TXREG
}
void camera_rx(void)
{
unsigned char x;
for(x=0 ; x<6 ; x++)
{
LCD_sendstring("before");
while(!PIR1bits.RCIF);
LCD_sendstring("after");
rx_buffer[x]=RCREG;
}
}
Last edited: