#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Send variable through USART


    Hi, I'm having trouble sending a variable through USART... PIC184520 with C18 complier. My goal is to generate a random number and send it over an sms. So I need to send it through USART to the GSM modem...

    Right now I'm either getting nothing or garbage values for this. I'm testing it by displaying this over hyperterminal first.

    Code:
    #include <p18F4520.h>
    #include <delays.h>
    #include <stdio.h>
    #include <usart.h>
    #include <stdlib.h>
    #include <system4520.h>
    
    void usart_init();
    
    void main(void)
    {
    	
    	int num1 = rand() % 100; 
    	int z = 0x1a;
    		
    	usart_init();
    
    	putrsUSART("AT\r\n");
    	Delay10KTCYx(50);
    	putrsUSART("AT+CMGF=1\r\n");
    	Delay10KTCYx(50);
    	putrsUSART("AT+CMGS=0123310587\r\n");
    	Delay10KTCYx(50);
    	putsUSART(num1);
    	Delay10KTCYx(50);
    	putcUSART(z);
    	Delay10KTCYx(50);
    	while(1);
    }
    
    void usart_init(void)
    { 
    	OpenUSART(USART_TX_INT_OFF &		//Transmit interrupt off
    	USART_RX_INT_ON &			//Receive interrupt on
    	USART_ASYNCH_MODE &			//Asynchronous mode
    	USART_EIGHT_BIT &			//8-bit data
    	USART_CONT_RX &				//Continuous reception
    	USART_BRGH_HIGH,25);//9600 baud3
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    Perhaps you should use sprintf to format your number as a string, and then send that string.

    Sending any random byte ranging from 0 to 99 (especially when one possible value is the same as int z = 0x1a) is sure to confuse things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo