Hi,
I am relatively new to C programming and completely new to USART operation.
I am trying to measure an analogue input in one PIC then allocate these values into 7 different classes depending on their value. I then want to assign an identifier to each class, in this case, the 0, 1, 2, 3 and so on. With these numbers, I want to send them to another PIC connected via a RF 433mhz wireless module. When the data is received on the other side, this will correspond to a series of LEDS which should illuminate when the ADC input changes.
I have a start on the code using my existing code that worked without the USART wireless included and using the PIC16F88 datasheet.
Any help as to why this isnt working would be much appreciated.
Here is the transmitter code:
Code:
//PIC 16F88 microcontroller
#include <pic.h>
#include "delay.c"
#include "delay.h"
__CONFIG(WDTDIS & INTIO & PWRTEN & MCLRDIS & UNPROTECT & BORDIS);
main()
{
int ADCresult= 0;
CMCON = 7; //Turn off comparators
ANSEL = 0b00000100; //Pin 11- RA2/AN2 as analogue input.
TRISA= 0b00000100; //PortA as output except RA2 as input
TRISB = 0; //PortB as output
//OSCCON= 0b01111110; //This is to set the internal OSC to 8 Mhz
ADCON0= 0b00001001; //Result left justified format, voltage reference= VDD,
//AN2= analogue input, initiates ADC and waiting to start conversion.
ADCON1= 0b00010000; //For 4 Mhz, 8Tosc= 2 uS, which > 1.6 uS, the minimum requirement.
SPBRG = 25;
BRGH = 1; // speed USART, high speed - baudrate more acurate
SYNC = 0; // async mode
SPEN = 1; // serial port enable bit
// transmit
TXIE = 0; // initialize transmit
TX9 = 0; // disabled 9 bit transmission
TXEN = 0;
TXEN = 1; // transmit enable bit
TXREG =0;
while(1)
{
DelayUs(50); //Wait minimum sample time.
GODONE = 1; //Start conversion.
while(GODONE== 1); //Wait until conversion is done.
ADCresult= ADRESH; //Transfer from ADRESH to ADCresult.
if(ADCresult<= 76){
TXREG= 0x0;
}
else if(ADCresult<= 102){
TXREG= 0x1;
}
else if(ADCresult<= 128){
TXREG= 0x2;
}
else if(ADCresult<= 153){
TXREG= 0x3;
}
else if(ADCresult<= 179){
TXREG= 0x4;
}
else if(ADCresult<= 204){
TXREG= 0x5;
}
else if(ADCresult<= 255){
TXREG= 0x6;
}
}
}
and here is the receiver code:
Code:
//PIC 16F88 microcontroller
#include <pic.h>
#include "delay.c"
#include "delay.h"
__CONFIG(WDTDIS & INTIO & PWRTEN & MCLRDIS & UNPROTECT & BORDIS);
const char display_portb[]= {0b00000001, 0b00000010, 0b00001000, 0b00010000,
0b01000000, 0b10000000};
main()
{
SPBRG = 25;
BRGH = 1; // speed USART, high speed - baudrate more acurate
SYNC = 0; // async mode
SPEN = 1; // serial port enable bit
// receive
CREN = 1; // enable continues receive
RCIE = 0; // interrupt cleared
RX9 = 0; // disabled 9 bit reception
ADDEN = 0; // geen adress detectie (9e bit)
RCREG = 0;
while(1)
{
if(RCREG=0x0)
{
PORTB= 0;
}
else if(RCREG=0x1) PORTB= display_portb[0];
else if(RCREG=0x2) PORTB= display_portb[1];
else if(RCREG=0x3) PORTB= display_portb[2];
else if(RCREG=0x4) PORTB= display_portb[3];
else if(RCREG=0x5) PORTB= display_portb[4];
else if(RCREG=0x6) PORTB= display_portb[5];
}
}
Thanks
C.Fry