Con questo codice dovrebbe funzionare
#include <pic16f870.h> //libreria standard di riconoscimento hardware
#include <stdio.h>
#define _XTAL_FREQ 20000000
#define BAUDRATE 9600 //bps
#include <xc.h>
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ALL // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)
#define LED0 PORTBbits.RB0
#define LED1 PORTBbits.RB1
#define LED2 PORTBbits.RB2
#define LED3 PORTBbits.RB3
#define LED4 PORTBbits.RB4
#define DEVICEADDRESS 0x01
#define STARTFRAME 0xFF
#define Time25_msec 125
//--------------------------------------------------------------------------------
//Function Prototypes
void InitUART(void);
void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);
void Par_ser(void);
volatile unsigned char N_byte;
volatile unsigned char rxchar = 0; // Variable per il byte ricevuto
volatile unsigned char rxarray[8]; // array per i byte ricevuti
unsigned char i=0;
volatile unsigned char ready=0;//Se il comando è pronto
volatile unsigned int Timer25_msec=0;
volatile unsigned char TimeoutRx=0;
volatile unsigned char Command=0;
//--------------------------------------------------------------------------------
void __interrupt () isr_routine (void){
if(RCIF){ // Se ricevuto carattere
rxchar = ReceiveByteSerially(); //Legge il byte ricevuto serialmente
rxarray[i++] = rxchar;//Lo memorizza nell'array
if((N_byte)==(i+1)){
ready = 1; //Il comando è memorizzato nell'array
RCIE = 0; //disabilita ricezione caratteri da seriale
PEIE = 0; //disabilita interrupt delle periferiche
GIE = 0; //disabilita interrupt globale
}
RCIF=0;//Azzera flag
Timer25_msec=Time25_msec;//Carica variabile timeout
}
if(INTCONbits.TMR0IF == 1){//dopo 200 usec
if(Timer25_msec>0){
if((--Timer25_msec==0)&&(!ready)){//dopo 25 msec
TimeoutRx=1;//Setta bit
}
}
TMR0=6;//Load Timer0 for 25 msec Overflow
INTCONbits.TMR0IF = 0;
}
}
void SendByteSerially(unsigned char Byte){ // Writes a character to the serial port
while(!TXIF); // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char ReceiveByteSerially(void) //Legge carattere da porta seriale
{
if(OERR){ // If over run error, then reset the receiver
CREN = 0;
CREN = 1;
}
while(!RCIF); // Wait for transmission to receive
return RCREG;
}
void ClearBufferRx(){
char index;
for(index=0;index<8;index++){
rxarray[index]=0;
}
i=0;
}
void InitUART(void){
TRISC6 = 0; // TX Pin
TRISC7 = 1; // RX Pin
SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TX9 = 0; // 8-bit transmission
RX9 = 0; // 8-bit reception
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
void Par_ser(void) //Analisi comandi
{
switch (Command) {
case 0x04: // Comando Pan Left
LED0 = 0;
LED1 = 1;
break;
case 0x02: // Comando Pan Right
LED0 = 1;
LED1 = 0;
break;
case 0x08: // Comando Tilt Up
LED2 = 0;
LED3 = 1;
break;
case 0x10: // Comando Tilt Down
LED2 = 1;
LED3 = 0;
break;
case 0x00: //Comando di stop
LED0 = 0;
LED1 = 0;
LED2 = 0;
LED3 = 0;
break;
}
}
void main(void) //inizia la funzione principale
{
TRISA = 0x00; // tutte uscite per evitare interferenze
TRISB = 0x00; // uscite comandi
TRISC = 0b10000000; // bit 6-7 Seriale
PORTA = 0x00; // azzera le porte
PORTB = 0x00; // azzera il PORT B
PORTC = 0x00;
// Var_Init(); //inizializza le variabili di trasmissione seriale
ADCON1= 0x87; // PORTA all digital mode
//Inizializzazione uart
TXSTA= 0x24; // TX abilitato
RCSTA= 0X90; // Registro RX USART
BRGH = 1; // USART alta velocitÃ
SPBRG= 129; // Generatore di Baud Rate (25=9600 baud)
SPEN=1;
TXEN=1;
//Inizializzazione interrupt
INTCON = 0; // Interrupt disabilitato
PIE1 = 0; // Interrupt di periferica disabilitati
GIE = 1; // Global Interrupt Enable
PEIE = 1; // Periferal Interrupt Enable
RCIE = 1; // USART Receive Interrupt Enable
CREN=1;
RCIF=1;
N_byte=7;
INTCONbits.TMR0IE=1;// Enable interrupt on TMR0 overflow
INTCONbits.TMR0IF = 0;//Azzeramento flag interrupt Timer0
TMR0=6;//Load Timer0 for 25 msec Overflow
OPTION_REG=1;//Timer0 acceso,prescaler /4,oscillatore
InitUART(); // Initialize UART
ClearBufferRx();
TRMT=1;
// SendByteSerially('
');
// SendByteSerially('
');
Timer25_msec=Time25_msec;//Timer per 25 msec
for (;; ) {
if(ready){//Se il dato è pronto
if((rxarray[0]==STARTFRAME)&&(rxarray[1]==DEVICEADDRESS)){
Command=rxarray[3];
Par_ser();
}
// SendByteSerially(rxarray[0]);
// SendByteSerially(rxarray[1]);
// SendByteSerially(rxarray[2]);
// SendByteSerially(rxarray[3]);
// SendByteSerially(rxarray[4]);
// SendByteSerially(rxarray[5]);
// SendByteSerially(rxarray[6]);
ClearBufferRx();//Azzera buffer di ricezione
PEIE = 1; //enable peripheral interrupt bit
GIE = 1; //enable global interrupt bit
RCIE = 1; //Abilita ricezione carattere ad interrupt
ready=0; //Azzera dato pronto
}
}
if(TimeoutRx){//Se sono trascorsi più di 25 msec
TimeoutRx=0;//Azzera variabile per ilTimeout
ClearBufferRx();//Azzera buffer di ricezione
}
return;
} // Fine del main