I work with pic18f4550 MCU and use mikro c pro IDE , I have programming code that use UART module and turn on led when message "led1 = on" sends to UART module and when sends "led1 = off" the led turn off , it works good, but the problem is when I press MCR button to reset the code,the pic mcu don't do that,it is stay at reset operation , i need to restart simulation to send message "wellcome again.
on the other side , when i display a a welcome message on lcd without using UART module and press restart the MCU dosen't remain at reset operation and welcome message appear that.
my code with UART:
Code: Select all
// LCD module connections
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB0_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 TRISB1_bit;
sbit LCD_EN_Direction at TRISB0_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
//leds connection declerations:
sbit led1 at RD0_bit;
sbit led1_Direction at TRISD0_bit;
sbit led2 at RD1_bit;
sbit led2_Direction at TRISD1_bit;
sbit led3 at RD2_bit;
sbit led3_Direction at TRISD2_bit;
//***********************************
//***********************************
//buttons connections:
sbit btn1 at RD3_bit;
sbit btn1_Direction at TRISD3_bit;
sbit btn2 at RD4_bit;
sbit btn2_Direction at TRISD4_bit;
sbit btn3 at RD5_bit;
sbit btn3_Direction at TRISD5_bit;
sbit motor at RC2_bit;
sbit motor_Direction at TRISC2_bit;
sbit rxt at RC7_bit;
sbit rxt_Direction at TRISC7_bit;
sbit txt at RC6_bit;
sbit txt_Direction at TRISC6_bit;
//**************************************
//***************************************
#define CR 0x0D
#define LF 0x0A
char temp;
char rx_buff[50];
char data_ready ;
unsigned int data_len;
unsigned short speed=0;
char speed_txt[4];
//config uart communication
void uart_config()
{
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
rxt_Direction=1;
txt_Direction=0;txt=0;
PIE1.RCIE=1; //Enabe RX interrupts
PEIE_bit=1; //prepheral interrupt enable bit
GIE_bit=1; //global interrupt enable bit
}
//**********************************************
//**********************************************
void config()
{
//se off analog coparator
CMCON = 0X07;
//set AN6 TO AN6 TO AN10
ADCON1=0b00001000;
uart_config();
//set leds as output
led1_Direction=0; led1=0;
led2_Direction=0; led2=0;
led3_Direction=0; led3=0;
//set buttons as input
btn1_Direction=1;
btn2_Direction=1;
btn3_Direction=1;
//motor pin as output:
motor_Direction =0; motor=0;
//config pwm
pwm1_init(500);
pwm1_set_duty(speed);
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
}
//reset the buffer
void reset_buff(){
memset(rx_buff,0,data_len);
data_len=0;
data_ready=0;
}
void find_get_string(char *string,char *from,char _from,char *to,char _to, char *get_text)
{
char *p1;
char *p2;
char length=0;
p1=strstr(string,from) + _from;
p2=strstr(p1,to) + _to;
length=p2-p1;
strncpy(get_text,p1,length);
get_text[length] = '\0';
}
//togle led from serial message(python)
void toggle_led(){
if(data_ready==1){
if(strstr(rx_buff,"led1 = on")){
led1=1;
UART1_Write_Text("led1 is on ");
reset_buff ();
UART1_Write_Text("\r\n");
}
if(strstr(rx_buff,"led1 = off")){
led1=0;
UART1_Write_Text("\r\n");
reset_buff ();
}
}
}
void main() {
config();
reset_buff();
lcd_out(1,1,"welcome");
uart1_write_text("hellow");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
while(1){
toggle_led();
}
}
//interrupt service routine
void interrupt()
{
if(PIR1.RCIF==1)
{
temp = RCREG; //uart1_read();
TXREG = temp;
if(temp==CR || temp==LF)
{
if(data_len>1)
{
//send CR & LF
TXREG=CR;
TXREG=LF;
rx_buff[data_len]='\0';
data_ready=1;
}
}
else
{
rx_buff[data_len]=temp;
data_len++;
}
PIR1.RCIF==0; //RESET UART FLAG
}
}
the code after remove UART CONFIG
Code: Select all
void main() {
config();
lcd_out(1,1,"welcome");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
while(1){
lcd_out(1,1,"loop");
}
}
Code: Select all
void config()
{
//se off analog coparator
CMCON = 0X07;
//set AN6 TO AN6 TO AN10
ADCON1=0b00001000;
//set leds as output
led1_Direction=0; led1=0;
led2_Direction=0; led2=0;
led3_Direction=0; led3=0;
//set buttons as input
btn1_Direction=1;
btn2_Direction=1;
btn3_Direction=1;
//motor pin as output:
motor_Direction =0; motor=0;
//config pwm
pwm1_init(500);
pwm1_set_duty(speed);
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
}