I am new to writing firmware for 8-bit PICs and can use some help with my code. I am using PIC16F1829 for an LED module that receives RX commands. I'm just trying to set up the basics of how to turn on the LEDs when a certain value is received at the RX pin, but can't even get it.
I would like to make UART work through interrupts, but I can’t even make it work with polling in the main loop. My interrupt vector is commented out in the code below.
RX pin: RC5
TX pin: RB7
Turning On / Off Indicators On / Off: RA5
Pin RA5 works great to turn LEDs on and off. The TX contact works, although I did not confirm that the TXIF interrupt also does not work, as RCIF does not work.
I tried reading RCIF and PIR1bits.RCIF. Both are compiled. None of them worked. I tried this on two different PICs on two different LED modules. They turn on, but reading the RX pin does not work either.
The RXIN variable is initially defined as 3 and, therefore, due to the RXIN cycle-- in the main loop, the indicators flash 3 times at startup, so I know that it enters the main loop. But as far as I can tell, the RCIF interrupt does not work when it receives on the RX pin.
, RX TX- , , (300 , 8N1.) , RX 5V . RCIF . - , , .
:
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#define _BAUD_PRESCALER_LOW_ 0x2A
#define _BAUD_PRESCALER_HIGH_ 0x68
#define _XTAL_FREQ 32000000
#pragma config FOSC = INTOSC
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config CLKOUTEN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF
#pragma config WRT = OFF
#pragma config PLLEN = ON
#pragma config STVREN = ON
#pragma config BORV = LO
#pragma config LVP = OFF
int flagRXFramingError = 0;
int flagRXOverrunError = 0;
volatile unsigned char RXIN = 3;
unsigned char UARTRead(){
return RCREG;
}
void writeRXIN(unsigned char a){
RXIN = a;
}
void TX(unsigned char a){
while(!TXIF){}
TXREG = a;
}
int main(int argc, char** argv) {
OSCCON = 0xF0;
OSCTUNE = 0x00;
while(PLLR == 0)
{
}
WDTCON = 0x16;
__delay_ms(5);
GIE = 1;
__delay_ms(5);
PEIE = 1;
__delay_ms(5);
RCIE = 1;
__delay_ms(5);
TXIE = 1;
__delay_ms(5);
ADIE = 1;
__delay_ms(5);
RXDTSEL = 0;
__delay_ms(5);
TXCKSEL = 0;
__delay_ms(5);
TRISC5 = 1;
__delay_ms(5);
SPEN = 1;
__delay_ms(5);
SYNC = 0;
__delay_ms(5);
RX9 = 0;
__delay_ms(5);
TX9 = 0;
__delay_ms(5);
CREN = 1;
__delay_ms(5);
TXEN = 1;
__delay_ms(5);
BRG16 = 1;
__delay_ms(5);
BRGH = 1;
__delay_ms(5);
ABDEN = 0;
__delay_ms(5);
SPBRGH = _BAUD_PRESCALER_HIGH_;
__delay_ms(5);
SPBRGL = _BAUD_PRESCALER_LOW_;
__delay_ms(5);
TRISC6 = 0;
__delay_ms(5);
ANSC6 = 0;
__delay_ms(5);
TRISA5 = 0;
__delay_ms(5);
LATC6 = 1;
__delay_ms(5);
while(1){
if(PIR1bits.RCIF){
writeRXIN(UARTRead());
TX(RXIN);
}
while(RXIN > 0){
RA5 = 1;
__delay_ms(100);
RA5 = 0;
__delay_ms(100);
RXIN--;
}
}
RA5 = 1;
return (EXIT_SUCCESS);
}