Cant figure out the uart prob of pic16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by kuannygohcheetatt, Feb 11, 2014.

  1. kuannygohcheetatt

    Thread Starter Member

    Oct 31, 2013
    61
    0
    //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?
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you done some debugging in the software simulator. It should be able to tackle this problem
     
  3. kuannygohcheetatt

    Thread Starter Member

    Oct 31, 2013
    61
    0
    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??
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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
    Code ( (Unknown Language)):
    1. while (1) {
    2.   //TODO: write code
    3.  
    4. }
    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.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  6. kuannygohcheetatt

    Thread Starter Member

    Oct 31, 2013
    61
    0
    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.
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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: Feb 11, 2014
  8. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    No. If your code does not have a while(1) infinite loop your code will crash!
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Thread merged into one thread.
     
  10. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    "sprintf" need a format specifier.
     
Loading...