Sunday, January 12, 2014

Advanced USART Functions


In the previous post the USART_Transmit_char function can only send one character at a time. If you want to send a whole sentence or an integer or a non-integer you cant use the above function. Here are some code snippets that come in handy while using USART.
This function can send multiple characters at a time -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void USART_Transmit(char* txdata)
{
    int i = 0;
    while(txdata[i] != '')
    {
        USART_Transmit_char(txdata[i]);
        i++;
    }

}
Sending an integer -
NOTE : This function can only send three digit integers.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void USART_Transmit_int(unsigned int i)
{
        unsigned char h,t,o;
        o= (i%10) | 0x30;//ones
        i/=10;
        t= (i%10) | 0x30;//tens
        i=i%100;
        h=(i/10) | 0x30;//hundreds
        USART_Transmit_char(h);
        USART_Transmit_char(t);
        USART_Transmit_char(o);
}
Sending a float-
NOTE : Can only send xxx.xx type number
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void USART_Transmit_float(const float f)
{
        unsigned int v,p;
        long int num;
        num=f*1000;
        p=num%1000;
        num=num/1000;
        v=num;

        USART_Transmit_int(v);
        USART_Transmit_char('.');
        USART_Transmit_int(p);

}

No comments :

Post a Comment