Maker Pro
Maker Pro

simple usart problem

Code:
//Laboratory No:1
//File name:Lab1.c
//Student Name:Goh Chee Tatt

#define XTAL_FREQ 4MHZ


#include <htc.h>
#include "delay.h"
#include "lcd.h"
#include "usart.h"

#include <stdio.h>
#include <stdlib.h>



#define _XTAL_FREQ 4000000


/*Lab1.c - Turn ON PORT B 0 LED*/

__CONFIG(0x3f31);
unsigned long int i;

delay1s() {
for (i = 0; i < 30000; i++) {
NOP(); //no operation is carried out
}
}
unsigned char
getch() {
/* retrieve one byte */
while (!RCIF) /* set when register is not empty */
continue;
return RCREG;
}
void transmit_uart(const char *value) {

for(int i = 0; value != '\0'; i++) {
while(TXIF == 0) {}
TXREG = value;

}
}

main() {
TRISB = 0; //initialize portb as output
PORTB = 0; //set all pin of portb as low
TRISD = 0;
PORTD = 0;
lcd_init();
init_comms();
SPBRG = 25; //4MHz
TXSTA = 0x24;
RCSTA = 0x90;
//USART interrupt
RCIE = 1;
GIE = 1;
PEIE = 1;
char line[50];


char str [20];
char hai[10];
char hello[10];
char sai[10];
char jiak[10];
char you[10];

int n;

i=0;

n = sprintf(hai, "atmy1111");





transmit_uart(hai);
__delay_ms(200);


__delay_ms(10);



}
void interrupt usart(void) {
char go[50];
if (RCIE && RCIF) {


i=0;
go = getch();



lcd_putch(go);


}


}
Code:


hi guys i facing a problem in usart, i perform a loop test on my microcontroller , means sending data and receive data within a single microcontroller, however, when simulated using proteus software, i noticed that i received repeat number of same data, why it is so , doesnt i only send the data once? can someone explain?
 

KrisBlueNZ

Sadly passed away in 2015
Your interrupt handler has to clear the RCIF flag. If you don't do that, as soon as you return from your interrupt handler, the interrupt will trigger again.

You should not be calling lcd_putch() from inside your interrupt handler. You should use a global array with global offsets or pointers. Your interrupt handler should put characters into the array and update the 'put' pointer, and your mainline should take characters out and update the 'get' pointer. The mainline should send the characters to the LCD.

Google circular buffer or queue for details.
 
Just to add a little to Kris's comment...

First, I am not conversant in C and am not sure what your processor is, but assuming it is a PIC (Microchip), the only way to clear the RCIF flag is to read the RCREG until it is empty.

Code:
/* retrieve one byte */
while (!RCIF) /* set when register is not empty */continue;
return RCREG;

Since it is a FIFO with two bytes, you need to read twice or, read and check. The above code may do that, I am not certain. However,if a third full byte is received you will get an overflow error (OERR), which you need to check for and clear by a reset using either CREN or SPEN. There are differences between the older USART and EUSART. I have been using SPEN with a EUSART.

John
 
Last edited:
Top