Hayee
Member level 4
HI guys
I am doing USART communication between two microcontrollers, one is PIC18f46k22 and other is PIC16f883.
PIC18F46K22 is running on internal oscillator with system clock of 16Mhz. USART communication uses Asynchronous Mode with Baud Rate of 9600.
PIC16F883 is running on internal oscillator with system clock of 8Mhz. USART communication uses Asynchronous Mode with Baud Rate of 9600.
I am using MPLAB version 6.15 and Proteus for simulation purpose.
I made a simple program in which PIC18F46K22 first send a byte with a value of 1.
when PIC16F883 receive this value 1 then it will send two byte to PIC18F46k22, then according to the value receive PIC18F46K22 toggle the port pins.
Up till now the code is working fine but I am wondering when I add further stuff in my both microcontrollers then how do they behave like there should be some other routines in both microcontrollers, delays, execution time will be different. What I think they will loose data because when PIC18F46K22 send data maybe PIC16F883 will be in another loop/function and miss the data, vice-versa
What will be the possible solutions then.
I have attached both code here for your reference. Kindly take a look and tell me how I move further so that I can't loose any data.
Also guide me if I am missing something in my code which is necessary
code of PIC18F46K22
code of PIC16F883
Thanks
I am doing USART communication between two microcontrollers, one is PIC18f46k22 and other is PIC16f883.
PIC18F46K22 is running on internal oscillator with system clock of 16Mhz. USART communication uses Asynchronous Mode with Baud Rate of 9600.
PIC16F883 is running on internal oscillator with system clock of 8Mhz. USART communication uses Asynchronous Mode with Baud Rate of 9600.
I am using MPLAB version 6.15 and Proteus for simulation purpose.
I made a simple program in which PIC18F46K22 first send a byte with a value of 1.
when PIC16F883 receive this value 1 then it will send two byte to PIC18F46k22, then according to the value receive PIC18F46K22 toggle the port pins.
Up till now the code is working fine but I am wondering when I add further stuff in my both microcontrollers then how do they behave like there should be some other routines in both microcontrollers, delays, execution time will be different. What I think they will loose data because when PIC18F46K22 send data maybe PIC16F883 will be in another loop/function and miss the data, vice-versa
What will be the possible solutions then.
I have attached both code here for your reference. Kindly take a look and tell me how I move further so that I can't loose any data.
Also guide me if I am missing something in my code which is necessary
code of PIC18F46K22
C:
#include "mcc_generated_files/mcc.h"
uint8_t dataArray[2];
void Serial_Write(uint8_t data)
{
if(PIR1bits.TX1IF) TXREG1 = data; // Write the data byte to the USART.
}
void Serial_Read(void)
{
// If Over run error occurs then clear OERR flag, disable then enable
// the CREN flag.
if(RCSTAbits.OERR){
RCSTAbits.CREN = 0;
RCSTAbits.OERR = 0;
RCSTAbits.CREN = 1;
}
// receive the bytes here.
for(int a = 0; a < 2; a++){
if(PIR1bits.RCIF){
dataArray[a] = RCREG;
}
}
}
void main(void)
{
// Initialize the device like OSCILLATOR, PORTS, UART etc
SYSTEM_Initialize();
uint8_t pwmCounter = 0;
ANSELB = 0x00;
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
pwmCounter = 1;
while (1)
{
// Add your application code
Serial_Write(pwmCounter);
__delay_ms(10);
Serial_Read();
if(dataArray[0] == 2){
PORTBbits.RB0 = ~PORTBbits.RB0; dataArray[0] = 0;
}
if(dataArray[1] == 3){
PORTBbits.RB1 = ~PORTBbits.RB1; dataArray[1] = 0;
}
}
}
code of PIC16F883
C:
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 8000000
uint8_t DataArray[2];
void Serial_Write(uint8_t* data)
{
if(PIR1bits.TXIF){
for (int a = 0; a < 2; a++){
TXREG = data[a];
}
}
}
uint8_t Serial_Read(void)
{
if(RCSTAbits.OERR){
RCSTAbits.CREN = 0;
RCSTAbits.OERR = 0;
RCSTAbits.CREN = 1;
}
if(PIR1bits.RCIF){
return RCREG;
}
return 255;
}
int main() {
OSCCONbits.IRCF = 0b111; // Internal OCS with 16Mhz
TRISCbits.TRISC0 = 0;
TRISCbits.TRISC3 = 0;
PORTB = 0;
ANSELH = 0x00;
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
/*UART Settings*/
BAUDCTL = 0x08;
RCSTA = 0x90;
TXSTA = 0x24;
SPBRG = 0xCF;
SPBRGH = 0x00;
uint8_t counter = 0;
while(1){
DataArray[0] = 2;
DataArray[1] = 3;
counter = Serial_Read();
if(counter == 1) Serial_Write(DataArray);
}
}
Thanks