Cant figure out the uart prob of pic16f877a

Thread Starter

kuannygohcheetatt

Joined Oct 31, 2013
61
//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);


}


}


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?
 

Thread Starter

kuannygohcheetatt

Joined Oct 31, 2013
61
i notice that if i put a while loop under all those code in the main project, which the while loop basically does nothing, then i will be able to run all the code above the while loop once, but if i exclude the while loop all the code will be run many times, i am a nobbie in c and i was wondering isnt the program will exit the main instead on keep on runnig the main loop if there is no while(1) loop exists??
 

tshuck

Joined Oct 18, 2012
3,534
The controller may do odd things if it ever reaches the end of the program, that is why everything in main after initialization should be in a
Rich (BB code):
while (1) {
  //TODO: write code

}
loop.

Without it, the controller might jump to a random location in program memory and start executing, reset, or freeze (it looks to be resetting in your case). Always ensure your coffee never ends in an embedded system like this.
 

t06afre

Joined May 11, 2009
5,934
If you do not have any form of endless loop in you main function. The program counter just continue to execute the rest of the code in the ROM. Then it comes to the end of the ROM. The counter will roll over and start to execute at address 0 again.
 

Thread Starter

kuannygohcheetatt

Joined Oct 31, 2013
61
so basically problem = if there is infinite while(1) loop present , code above it will run once , if absent, all code in the main loop will be run numerous times.
 

tshuck

Joined Oct 18, 2012
3,534
See responses in the applicable thread.

The while (1) loop is an infinite loop, so it makes sense that nothing after it could happen...

Edit: It should be noted that this question is referring to an embedded context, given this thread.
 
Last edited:
Top