I think it is difficult to debug your program without the actual hardware.I can't just "guess" what is happening without thoroughly checking it, which I can't due to time constraint.Also, on your latest code above you made changes to the program structure that I had initially given.For example, putting "while()" loop on the rx_poll() function.rx_poll() should be unblocking and must return immediately to its caller avoiding delays and blocks.Putting while(1) puts your MCU into and endless loop.A tip also, is that you must keep a copy of a working program before making changes.This way you could revert back to the last working one if possible.Your current code has lots of bugs already and I think the previously working one after the initial corrections of my code is better.
#include <pic.h>#include "delay.h"#include "serial.h"//configuration fuses__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);#define MAX_KEYS 6char key_buffer[MAX_KEYS + 1];unsigned char key_counter = 0;const unsigned char led_map[7] = {0x00,0x01,0x03,0x07,0x0F,0x1F,0x3f};/* LED SEQUENCE INDICATOR */unsigned char scanned_key1 = 0x00;unsigned char scanned_key2 = 0x00;unsigned char decoded_key = 0x00;void init_LED(void){ TRISA0 = 0; RA0 = 0; /* OK LED */ TRISA1 = 0; RA1 = 0; /* STANDBY LED */ TRISA2 = 0; RA2 = 0; /* ERROR LED */ TRISA5 = 0; RA5 = 0; /* ALARM PIN */ TRISE0 = 0; RE0 = 0; /* DOOR LOCK */ TRISE1 = 0; RE1 = 0; /* TIME ENDS */ TRISE2 = 0; RE2 = 0; /* TIME ALARM */}void init_KEYPAD(void){ TRISD |= 0x70; /*PORTD<6:4> input, COLUMNS */ TRISD &= ~0x0F; /*PORTD<3:0> output, ROWS */ PORTD |= 0x0F; /*PORTD<3:0> initially high */}unsigned char decode_key(unsigned char val){ unsigned char inputted_key; switch(val) { case 0x60: //1 inputted_key = '1'; break; case 0x50: //2 inputted_key = '2'; break; case 0x30: //3 inputted_key = '3'; break; case 0x61: //4 inputted_key = '4'; break; case 0x51: //5 inputted_key = '5'; break; case 0x31: //6 inputted_key = '6'; break; case 0x62: //7 inputted_key = '7'; break; case 0x52: //8 inputted_key = '8'; break; case 0x32: //9 inputted_key = '9'; break; case 0x63: //* inputted_key = '*'; break; case 0x53: //0 inputted_key = '0'; break; case 0x33: //# inputted_key = '#' ; break; default: inputted_key = decoded_key; break; } return inputted_key; }unsigned char scan_keypad(){ unsigned char val_1 = 0x00; unsigned char val_2 = 0x00; unsigned char count1; for(count1=0;count1 < 4;count1++) { PORTD = ~(0x01 << count1); /*scan row*/ val_1 = PORTD & 0x70; /*read column, RD<6:4> */ /*while clearing RD<7> & RD<3:0> */ if (val_1 != 0x70) /*if a column is pressed */ { val_2 = (val_1 | count1); /*column (high-nibble) and count (low-nibble) */ return val_2; /*return value*/ } } return 0xFF; /*return any value*/ }void ALARM_delay(void){ DelayMs(500); /*1/2 seconds*/}void ERROR_delay(void){ DelayMs(3000); /*3 seconds*/}/* * Retrieve any received character from UART RX buffer. * WARNING: Blocking code */ #if 0char getch(void){ char c; /* * Clear any receive errors */ if(OERR) //check if there is error { CREN = 0; //clear CREN = 1; //re-enable } while(!RCIF) //wait while no character is received ; c = RCREG; return c;}#endifvoid rx_poll(void){ unsigned char val = 0x00; if(OERR) //check if there is error { CREN = 0; //clear CREN = 1; //re-enable } if (!RCIF) return; val = RCREG; switch(val) { case 'g': RA0 = 1; /* toggle OK LED INDICATOR */ puts("PIN CODE is CORRECT\r\n"); RA5 = 0; /* toggle Security ALARM OFF */ //puts("Security ALARM Deactivated\r\n"); RE0 = 1; /* toggle Door Lock ON ("Pin should be HIGH TO DISABLE DOOR LOCK") */ //puts("Room Lock is Dis Engage\r\n"); break; case 'G': RA0 = 0; /* toggle OK LED INDICATOR */ puts("PIN CODE is CORRECT\r\n"); RA5 = 1; /* toggle Security ALARM OFF */ //puts("Room Security ALARM Deactivated\n"); RE0 = 0; /* toggle Door Lock OFF ("Pin should be HIGH TO DISABLE DOOR LOCK") */ //puts("Room Lock is Engage\n"); break; case 'b': RA2 = 1; ERROR_delay(); RA2 = 0; /* toggle ERROR LED INDICATOR */ puts("PIN CODE is INCORRECT\r\n"); break; case 't': RE1 = 1; /* toggle TIME LIGHT INDICATOR */ puts("10 minutes before ROOM TIME ENDS\r\n"); break; case 'a': RE2 = 1; ALARM_delay(); RE2 = 0; RE1=0; puts("ROOM TIME ENDS\r\n"); break; }}void main(){ unsigned char i; ADCON1 = 0x07; /*Configure AN pins as digital*/ init_LED(); /*initialize PORTA1*/ init_UART(); /*initialize UART Connection*/ init_KEYPAD(); /*initialize PORTD*/ /*PORTD<3:0> as KEYPAD OUTPUT*/ /*PORTD<6:4> as KEYPAD INPUT*/ RA5 = 1; RA1 = 1; /*STANDBY LED INDICATOR*/ TRISB &= 0xC0; /* make RB0-RB5 as outputs for LEDs*/ PORTB = 0; /* clear LEDs */ while(1) { rx_poll(); scanned_key1 = scan_keypad(); if (scanned_key1 != 0xFF) { /* debounce */ DelayMs(50); scanned_key2 = scan_keypad(); if (scanned_key2==scanned_key1) { decoded_key = decode_key(scanned_key2); } /* wait for button release */ while (scan_keypad() != 0xFF) continue; switch (decoded_key) { case '*': key_counter = 0; break; case '#': for (i=0;i<key_counter;i++) putch(key_buffer[i]); //puts("\n"); key_counter = 0; break; default: if (key_counter<MAX_KEYS) { key_buffer[key_counter] = decoded_key; key_counter++; } } /* show LED bargraph */ PORTB = led_map[key_counter & 0x3F]; } } }
Here:Code: [Select]#include <pic.h>#include "delay.h"#include "serial.h"//configuration fuses__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);#define MAX_KEYS 6char key_buffer[MAX_KEYS + 1];unsigned char key_counter = 0;const unsigned char led_map[7] = {0x00,0x01,0x03,0x07,0x0F,0x1F,0x3f};/* LED SEQUENCE INDICATOR */unsigned char scanned_key1 = 0x00;unsigned char scanned_key2 = 0x00;unsigned char decoded_key = 0x00;void init_LED(void){ TRISA0 = 0; RA0 = 0; /* OK LED */ TRISA1 = 0; RA1 = 0; /* STANDBY LED */ TRISA2 = 0; RA2 = 0; /* ERROR LED */ TRISA5 = 0; RA5 = 0; /* ALARM PIN */ TRISE0 = 0; RE0 = 0; /* DOOR LOCK */ TRISE1 = 0; RE1 = 0; /* TIME ENDS */ TRISE2 = 0; RE2 = 0; /* TIME ALARM */}void init_KEYPAD(void){ TRISD |= 0x70; /*PORTD<6:4> input, COLUMNS */ TRISD &= ~0x0F; /*PORTD<3:0> output, ROWS */ PORTD |= 0x0F; /*PORTD<3:0> initially high */}unsigned char decode_key(unsigned char val){ unsigned char inputted_key; switch(val) { case 0x60: //1 inputted_key = '1'; break; case 0x50: //2 inputted_key = '2'; break; case 0x30: //3 inputted_key = '3'; break; case 0x61: //4 inputted_key = '4'; break; case 0x51: //5 inputted_key = '5'; break; case 0x31: //6 inputted_key = '6'; break; case 0x62: //7 inputted_key = '7'; break; case 0x52: //8 inputted_key = '8'; break; case 0x32: //9 inputted_key = '9'; break; case 0x63: //* inputted_key = '*'; break; case 0x53: //0 inputted_key = '0'; break; case 0x33: //# inputted_key = '#' ; break; default: inputted_key = decoded_key; break; } return inputted_key; }unsigned char scan_keypad(){ unsigned char val_1 = 0x00; unsigned char val_2 = 0x00; unsigned char count1; for(count1=0;count1 < 4;count1++) { PORTD = ~(0x01 << count1); /*scan row*/ val_1 = PORTD & 0x70; /*read column, RD<6:4> */ /*while clearing RD<7> & RD<3:0> */ if (val_1 != 0x70) /*if a column is pressed */ { val_2 = (val_1 | count1); /*column (high-nibble) and count (low-nibble) */ return val_2; /*return value*/ } } return 0xFF; /*return any value*/ }void ALARM_delay(void){ DelayMs(500); /*1/2 seconds*/}void ERROR_delay(void){ DelayMs(3000); /*3 seconds*/}/* * Retrieve any received character from UART RX buffer. * WARNING: Blocking code */ #if 0char getch(void){ char c; /* * Clear any receive errors */ if(OERR) //check if there is error { CREN = 0; //clear CREN = 1; //re-enable } while(!RCIF) //wait while no character is received ; c = RCREG; return c;}#endifvoid rx_poll(void){ unsigned char val = 0x00; if(OERR) //check if there is error { CREN = 0; //clear CREN = 1; //re-enable } if (!RCIF) return; val = RCREG; switch(val) { case 'g': RA0 = 1; /* toggle OK LED INDICATOR */ puts("PIN CODE is CORRECT\r\n"); RA5 = 0; /* toggle Security ALARM OFF */ //puts("Security ALARM Deactivated\r\n"); RE0 = 1; /* toggle Door Lock ON ("Pin should be HIGH TO DISABLE DOOR LOCK") */ //puts("Room Lock is Dis Engage\r\n"); break; case 'G': RA0 = 0; /* toggle OK LED INDICATOR */ puts("PIN CODE is CORRECT\r\n"); RA5 = 1; /* toggle Security ALARM OFF */ //puts("Room Security ALARM Deactivated\n"); RE0 = 0; /* toggle Door Lock OFF ("Pin should be HIGH TO DISABLE DOOR LOCK") */ //puts("Room Lock is Engage\n"); break; case 'b': RA2 = 1; ERROR_delay(); RA2 = 0; /* toggle ERROR LED INDICATOR */ puts("PIN CODE is INCORRECT\r\n"); break; case 't': RE1 = 1; /* toggle TIME LIGHT INDICATOR */ puts("10 minutes before ROOM TIME ENDS\r\n"); break; case 'a': RE2 = 1; ALARM_delay(); RE2 = 0; RE1=0; puts("ROOM TIME ENDS\r\n"); break; }}void main(){ unsigned char i; ADCON1 = 0x07; /*Configure AN pins as digital*/ init_LED(); /*initialize PORTA1*/ init_UART(); /*initialize UART Connection*/ init_KEYPAD(); /*initialize PORTD*/ /*PORTD<3:0> as KEYPAD OUTPUT*/ /*PORTD<6:4> as KEYPAD INPUT*/ RA5 = 1; RA1 = 1; /*STANDBY LED INDICATOR*/ TRISB &= 0xC0; /* make RB0-RB5 as outputs for LEDs*/ PORTB = 0; /* clear LEDs */ while(1) { rx_poll(); scanned_key1 = scan_keypad(); if (scanned_key1 != 0xFF) { /* debounce */ DelayMs(50); scanned_key2 = scan_keypad(); if (scanned_key2==scanned_key1) { decoded_key = decode_key(scanned_key2); } /* wait for button release */ while (scan_keypad() != 0xFF) continue; switch (decoded_key) { case '*': key_counter = 0; break; case '#': for (i=0;i<key_counter;i++) putch(key_buffer[i]); //puts("\n"); key_counter = 0; break; default: if (key_counter<MAX_KEYS) { key_buffer[key_counter] = decoded_key; key_counter++; } } /* show LED bargraph */ PORTB = led_map[key_counter & 0x3F]; } } }
It is very difficult to code and even debug without the hardware.Could you elaborate what didn't work.
kapag ang readtimeout is -1, it throws exception once wala siyang nakuhang data sa read buffer...pag readtimeout is 1000, it waits for 1000msec and pag wala pang data w/in that time, doon lang siya nagre raise ng exception.this applies sa mga serial read method na nag iimplement ng time out exception for serial port reading.
hello sir 7... i tried to simulate the mcu with hyperterminal and its working fine.. walang error... but with vb.net.. nag timeout sya if naka set sa 1 or 1000 ang readtimeout. and wala syang narrcv kpg naka -1
it looks like walang narereceive ang program mo... havent check your source code yet. baka bukas ko pa masilip yan...
wala nga sya na rrcv sir 7. kapag naka .readline sya lumalabasa ng readtimeout error... pagn aka readexisting.. narrcv nya pero balik sa dating problem na part-by-part ang data na rrcv.
wait for 1000msec (Sleep method in the Thread class inside the System.Threading namespace)
'------------------------------------- ' Event handler for the Data Received '------------------------------------- Private Sub DataReceived( _ ByVal sender As Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles SerialPort.DataReceived txtDataRcvPIN.Invoke(New _ myDelegate(AddressOf updateTextBox), _ New Object() {}) End Sub '------------------------------------------------------- ' Delegate and subroutine to update the Textbox control '------------------------------------------------------- Public Delegate Sub myDelegate() '-------------------------------------- ' Event handler for the Update Textbox '-------------------------------------- Public Sub updateTextBox() [b] Thread.Sleep(1000)[/b] txtDataRcvPIN.AppendText(SerialPort.ReadExisting) ' MessageBox.Show(txtDataRcvPIN.Text & ": " & txtDataRcvPIN.Text.Length) If txtDataRcvPIN.Text.Length = 7 Then 'recieved 6 digit pin data via serial port If SerialPort.IsOpen Then CheckPINCode(txtDataRcvPIN.Text) End If ElseIf txtDataRcvPIN.Text.Length < 6 Then 'Received less than 6 digit pin data via serial port If SerialPort.IsOpen Then SerialPort.Write("b") txtDataSend.AppendText("Invalid Password" & Environment.NewLine) End If End If With txtDataReceived .Font = New Font("Garamond", 10.0!, FontStyle.Bold) .AppendText(txtDataRcvPIN.Text) SerialPort.DiscardInBuffer() SerialPort.DiscardOutBuffer() .ScrollToCaret() End With txtDataRcvPIN.Clear() 'Timer1.Enabled = True End Sub
check for the bytesToRead property then pag di siya 0,execute the readexisting method para makita mo kung alin yung mga nareceive nyaPosted on: Today at 04:03:18 PM Posted by: reaver26