Easy-Downloader V1.1 with SDCC

[09-08 11:37:34]   来源:http://www.88dzw.com  微机|单片机   阅读:8650

文章摘要: } The pointer s points to the start address of string. It will send character to serial port with function putchar while the character is not the terminator byte!Sdcc has no putchar( ) function, so we must build it before we can use. I wrote a simple putchar( ) as my assembly

Easy-Downloader V1.1 with SDCC,标签:电路设计,http://www.88dzw.com
}
 

The pointer s points to the start address of string. It will send character to serial port with function putchar while the character is not the terminator byte!

Sdcc has no putchar( ) function, so we must build it before we can use. I wrote a simple putchar( ) as my assembly code.
  void putchar(char c)
{
 while(!TI); 
 TI=0;
 SBUF = c;
}
 

We test TI bit before we can write a byte to SBUF. While TI is not set (buffer is not free) keep polling it, when it set, clear it and write a byte to SUF.

The same as putchar( ) function, I had built the getchar( ) for this board to make it compatible with old version.
  char getchar(void)
{
 char c;
 while(!RI);
 RI =0;
 c = SBUF;
 putchar(c);    // echo to terminal
 return SBUF;
}
 

Now the code polls the RI bit, when it set, clear it and read SBUF and simply echo the received character with putchar( ) function.

The important function is getnum ( ) function. Let me explain how it works?
  unsigned int getnum()
{
    char s[6]; 
    char c;
    char i;
    unsigned int temp16; 
 c = 0;
 i=0;
  for (i = 0; c != 0xa; i++) // loop until CR has entered
    { 
        putchar(xon); // send xon to signal host to send byte
        c = getchar(); // get character from serial port
  if(c == 0xd) c=0xa; // convert CR to LF to make it compatible with ez31 and ez41
        s[i] = c; // save character to array
    }
    s[i-1] = 0; // put terminator at the end of string

// convert ascii to integer (atoi(s))
  temp16 = 0;
 for(i=0; s[i] != 0; i++) temp16 = 10*temp16 + s[i]-'0';
    return temp16; // return 16-bit for number of byte counting
}
 


 

Look at the red one, it is a simple for loop with condition to check the received character is NEWLINE or not. If not it will save the character being received to array s[i]. The putchar(xon) sends the xon to host to let it know the board is ready to receive a byte. The getchar( ) is not echo the received byte. However it converts CF to LF. When the loop found LF or 0xa, it will exit from the for loop and save terminator byte to s[i-1].

The ascii string that saved in array s[] will be converted to 16-bit number with atoi code.

上一页  [1] [2] [3] [4]  下一页


Tag:微机|单片机电路设计家电维修 - 单元电路介绍 - 微机|单片机

《Easy-Downloader V1.1 with SDCC》相关文章