This is the code
// PIC12F1822 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
#include <string.h>
#include <stdlib.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
char blink = 0;
void delay()
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<1000;j++);
}
}
void init()
{
OSCCON=0xF0; //32 Mhz
ANSELA=0x00; // All pins digital
BAUDCONbits.ABDOVF = 0;
BAUDCONbits.RCIDL=0;
BAUDCONbits.BRG16= 0;
BAUDCONbits.ABDEN = 0;
BAUDCONbits.SCKP = 0;
BAUDCONbits.WUE=0;
APFCONbits.RXDTSEL=0;
APFCONbits.TXCKSEL=0;
SPBRGL = 51;//Baud =9600
RCSTAbits.CREN=1;
RCSTAbits.SPEN =1; //Reciever enabled
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.RCIE = 1;
TXSTAbits.SYNC = 0;
TXSTAbits.TXEN = 1;
TXSTAbits.BRGH = 0;
TXSTAbits.TX9 = 0;
TRISAbits.TRISA0 = 0;
TRISAbits.TRISA1 = 1;
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA4 = 0;
}
void interrupt ISR()
{
if(PIR1bits.RCIF)
PIR1bits.RCIF = 0;
blink=RCREG;
PORTAbits.RA2 = 1;
delay();
PORTAbits.RA2 = 0;
delay();
}
void send_data(char a[])
{
int i;
for(i=0;i<strlen(a);i++)
{
TXREG = a[i];
while(!TXSTAbits.TRMT);
}
}
int main()
{
init();
while(1)
{
if(blink != 0)
{
delay();
send_data(blink);
delay();
blink = 0;
send_data("Dennis");
}
PORTAbits.RA4 = 1;
delay();
PORTAbits.RA4 = 1;
delay();
}
}
I am sending data from the UART of arduino.
the pins Ra2 is supposed to and does blink when it enters the interrupt.
the data Dennis is displayed correctly.
But the data recieved by the PIC is not sent back properly and im getting garbage at the serial port output.
Best Answer
The function
send_data()
expects a string, but you are giving it the single characterblink
. The program is probably trying to use the contents ofblink
as a pointer, and the result is whatever happens to be at that memory location (ie. garbage).You should change
blink
from typechar
to an array of characters, like this:-