2011-12-07 30 views
0
char uart_rd; 
int b; 

void main() { 
    ANSEL = 0;  
    ANSELH = 0; 
    C1ON_bit = 0; 
    C2ON_bit = 0; 
    TRISC = 0; 
    PORTC = 0x00; 
    UART1_Init(9600);    
    Delay_ms(10);     
    UART1_Write_Text("Start");   
    UART1_Write(10);    
    UART1_Write(13); 

    while (1) {      
    if (UART1_Data_Ready()) {  
     uart_rd = UART1_Read();  
     UART1_Write(uart_rd);  
     **WordtoInt(uart_rd, b)**- this line code is my error 

     if(b <= 20) 
     { 
       PORTC = 0x01; 
     } 
     if(b > 20)&&(b <= 40) 
     { 
       PORTC = 0x03; 
     } 
     if(b > 40)&&(b <= 60) 
     { 
       PORTC = 0x07; 
     } 
     if(b > 60)&&(b <= 80) 
     { 
       PORTC = 0x0F; 
     } 
     if(b > 80)&&(b <= 100) 
     { 
       PORTC = 0x1F; 
     } 


    } 
    } 
} 

這是我的任務。當我構建它時,它顯示我一個錯誤。microc中的任務錯誤圖片

我從0 to 100讀取數字,指示電池電量和取決於電量,port C0 to 5引腳將發光。

有人可以幫助我,如何找到將字轉換爲int的函數?

+1

什麼是錯誤信息? –

回答

1

uart_rd是一個字節的字符變量。 b是整型變量。要轉換uart_rd到b只是這樣做:

b = (int) uart_rd; 
+0

不需要/暗示的'(int)'cast的目的是什麼? –

+0

只是爲了防止警告級別設置爲高時的任何編譯器警告。 –

+0

只有當sizeof(int)= sizeof(char)和char是無符號字符時,纔會出現警告。不太可能的組合。 –

1

如何簡單地b = uart_rd;b = (unsigned char)uart_rd;