Author Topic: UART1_Write command issue in micro c  (Read 1087 times)

0 Members and 1 Guest are viewing this topic.

Offline NASKTopic starter

  • Contributor
  • Posts: 43
  • Country: lk
UART1_Write command issue in micro c
« on: August 25, 2017, 03:39:46 am »
I need to write variable(=a) value to UART like below using micro c. But UART1_Write(a) not showing value  of variable a. how fix this issue?   

Int a;
Void main (){
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
UART1_Write_Text("Start");   // show start text
while (1) {                     
   a= 10;
   UART1_Write(a);   }
}

 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: UART1_Write command issue in micro c
« Reply #1 on: August 25, 2017, 03:54:27 am »
I *ASS*U*ME* you mean MikroElektronika mikroC. Please be specific!   |O

Code: [Select]
UART1_Write(a);
outputs a SINGLE character of code a.  To output a number as ASCII characters you first need to convert the number to a zero terminated ASCII string in a buffer in RAM, then pass the pointer to that buffer to UART1_Write_Text().  For functions to convert a number to a string see the mikroC Conversions Library - which one to use and the size of the string you need to preallocate depends on the type of the number you are converting.
 

Offline NASKTopic starter

  • Contributor
  • Posts: 43
  • Country: lk
Re: UART1_Write command issue in micro c
« Reply #2 on: August 28, 2017, 12:51:59 pm »
I wrote a code without using MikroElektronika mikroC lib , this code useful for 0 to 99 usart TX 8)

#define _XTAL_FREQ 16000000
#define Baud_rate  9600

int a;
char buf[3];
void main() {

SPBRG = ((_XTAL_FREQ/64)/Baud_rate)-1;
TXSTA=0b00100010;
RCSTA.F7=1;

char *pyr=buf;

a=56;
  while (1) {

      if(a>=10){
    buf[0]=(a-a%10)/10  ;
    buf[1]=a%10;
    while(*pyr){

   TXREG=(*pyr+48);
   Delay_ms(10);
   pyr++;
   if(buf[1]==0){
   TXREG='0';    }

              }
              }
      else{
    buf[1]=(a-a%10)/10  ;
    buf[0]=a%10;
    while(*pyr){

   TXREG=(*pyr+48);
   Delay_ms(10);
   pyr++;     }
                 }
 
}
}

















 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf