kirangkr
Newbie level 6
- Joined
- Sep 19, 2014
- Messages
- 13
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 183
Hello All,
This is first time i'm posting a thread in edaboard, if i made any mistake please excuse me.
For my new project I'm using PIC18f24j11. I have configured UART and my TX is working fine. But my RX is not working. While debugging I saw no data is entering to RCREG register and hence I'm not getting interrupt. Please see my code and advice if i miss out something!
Best regards
Kirangkr
This is first time i'm posting a thread in edaboard, if i made any mistake please excuse me.
For my new project I'm using PIC18f24j11. I have configured UART and my TX is working fine. But my RX is not working. While debugging I saw no data is entering to RCREG register and hence I'm not getting interrupt. Please see my code and advice if i miss out something!
Best regards
Kirangkr
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 #include <htc.h> //PIC hardware mapping #include <p18f24j11.h> #include <xc.h> #include <delays.h> #include <usart.h> #include<stdio.h> #include <stdarg.h> #define _XTAL_FREQ 2000000 //FCPU =Fosc/4 #define FOSC 8000000 // Fosc 8MHz #pragma config WDTEN = OFF #pragma config STVREN = OFF #pragma config XINST = OFF #pragma config CP0 = OFF #pragma config OSC = INTOSC #pragma config T1DIG = OFF #pragma config LPT1OSC = OFF #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config DSBOREN = OFF #pragma config DSWDTEN = OFF #pragma config IOL1WAY = OFF /*****************************************************************************/ /* Local Macro or Enum Definition */ /*****************************************************************************/ #define PIR_DEBOUNCE 4 //400ms #define BLE_SLEEP_CTRL_PIN PORTCbits.RC4 #define PIR_STATUS PORTBbits.RB0 //typedef enum {FALSE = 0, TRUE = !FALSE} bool_t; typedef enum { PIR_STATE_FIRST_TRIGGER_CHECK = 0, PIR_STATE_INIT, PIR_STATE_COUNTING_LOW, PIR_STATE_COUNTING_HIGH } pir_state_t; /*****************************************************************************/ /* Local Function Declaration */ /*****************************************************************************/ void init_uart(void); void UART_TX(unsigned char x); void uart_send(void); pir_state_t pir_state_frist_trigger_check(void); pir_state_t pir_state_init(void); pir_state_t pir_state_counting_low(void); pir_state_t pir_state_counting_high(void); /*****************************************************************************/ /* Local Structure or Union Definition */ /*****************************************************************************/ //union Timers //{ // unsigned int lt; // char bt[2]; //}; pir_state_t (*p_pir_state[])(void) = { pir_state_frist_trigger_check, pir_state_init, pir_state_counting_low, pir_state_counting_high }; /*****************************************************************************/ /* Local Variable Definition */ /*****************************************************************************/ //bool_t is_report_time_reached = FALSE; pir_state_t pir_state = PIR_STATE_FIRST_TRIGGER_CHECK; unsigned int total_time = 0; unsigned int count_timer_value = 0; //100ms time base (timer3) unsigned int a = 0; unsigned int b = 0; unsigned int c = 0; unsigned int d = 0; unsigned int e = 0; unsigned char UART_DATA = 0x05; unsigned char counter = 0; unsigned char sec =0; unsigned short cntr = 0; unsigned char T1 = 0; int i =0; int k = 0; int frq = 0; char send[40]; char *ptr; volatile unsigned char Rx_data[40]; unsigned int time; void delay100ms(unsigned char value) { while(value--) { __delay_ms(100); } } void uart_tx_string(char *p_string) { while (*p_string != 0) { UART_TX((unsigned char)(*p_string)); p_string++; } } void main () { OSCCON =0b01110111; // 8Mhz internal oscillator PCFG12 = 1; PCFG11 = 1; PCFG10 = 1; PCFG9 = 1; PCFG8 = 1; TRISCbits.TRISC4 = 0; //Set RC4 as output pin; RC4 = 1; init_uart(); // TRISBbits.TRISB0 = 1; //Set RB0 as input pin; // RB0 = 0; // T1GCON = 0b1110000; TMR1GE = 0; T1CON = 0b10001111; //Fosc/4 and Prescale 1:1 TMR1H = 0; TMR1L = 0; IPEN =1; TMR1IF = 0; // Timer1 interrupt flag cleared TMR1IE = 1; //Timer1 interrupt enabled TMR1IP = 1; PEIE = 1; // Peripheral interrupt enabled GIE =1; // Enabled interrupt globally INT0IF = 0; //INT0 flag cleared INT0IE =1; INTEDG0 = 1; uart_tx_string("AT\r"); __delay_ms(300); uart_tx_string("ATA\r"); TMR0ON = 0; //Timer0 disabled while (1) { //RC4 = 0; if (frq == 1) { // RC4 = 1; __delay_ms(3); sprintf(send, "A = %d, B = %d, C = %d, D = %d, E = %d \r\n", a, b, c, d, e); uart_tx_string(send); __delay_ms(1); a = 0; b= 0; c=0; d = 0; e =0;frq =0; } RC4 = 0; sec =0; asm("sleep"); // } // Check PIR pir_state = (*p_pir_state[pir_state])(); } } void interrupt ISR() { if(RC1IF && RC1IE ) { // Rx_data = RCREG1; if(DataRdy1USART()) { Rx_data[i++] = Read1USART(); //read the byte from rx register if(i > 5) { i = 0; } RC1IF = 0; // clear rx flag } } if(TMR1IF) { cntr++; if (cntr >10) // check for 15 minutes { RC4 = 1; frq = 1; T1++; cntr =0; } TMR1IF = 0; } if (PIR2bits.TMR3IF) { PIR2bits.TMR3IF = 0; WriteTimer3(63897); count_timer_value++; } else if(INT0IF) { k = 1; INT0IF = 0; } else if(TMR0IE && TMR0IF) { counter++; // counter increment if (counter == 61) // check for 1 sec { sec++; counter = 0; // Reset counter } TMR0IF = 0; // Clear Flag } } void uart_send(void) { TXSTAbits.TXEN=1; // enable transmission ptr = send; // Initialize pointer while (*ptr) { // Loop until end of string if (TRMT == 1) { // Xmtr buffer empty? TXREG = *ptr; // Yes - load next char ptr++; // Point to next char } } } void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE { TRISCbits.TRISC7=1; //Make UART RX pin input TRISCbits.TRISC6=0; //Make UART TX pin output SPBRGH = 0x00; SPBRG = 0xCF; // SPBRG = 0x10; TXSTAbits.SYNC= 0; //0 = Asynchronous mode RCSTA1bits.SPEN=1; //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins) RC1IF = 0; //reset RX pin flag TX1IE = 0; //Disable TX interrupt RC1IE = 1; //Enable RX interrupt RC1IP = 1; //1 = high priority RCSTA1bits.CREN=1; //1 = Enables receiver BAUDCONbits.BRG16=1;//1 = 16-bit Baud Rate Generator ?SPBRG AND SPBRGH TXSTAbits.BRGH=1; //1 = HIGH Speed TXSTAbits.TXEN=1; //1 = Transmit enabled } void UART_TX(unsigned char c) { TXSTAbits.TXEN=0; // disable transmission TXREG=c; // load txreg with data TXSTAbits.TXEN=1; // enable transmission while(TXSTAbits.TRMT==0) // wait here till transmit complete { Nop(); } }
Last edited by a moderator: