SDCC 是一個開源免費的 C編譯器,且支持跨平臺,是在 Linux 下開發嵌入式( 51單片機 )應用的得力工具。但是其只有命令行的方式,使用起來諸多不方便。于是做了一個前端( SDCC 需要自行另外安裝),代碼編輯體驗比 Keil 好很多,支持 Linux、Windows 平臺,支持 x86、mips64el( loongson 3A ) 架構。
litmill.png (224.73 KB, 下載次數: 44)
下載附件
2023-7-3 10:53 上傳
對于一些壇友說的 SDCC 的兼容性問題,我想說只要注意以下幾點,基本沒啥問題。
(為了簡潔,下面的例子只截取關鍵部分代碼,完整的驗證程序在附件的 demo 里)
1、位取反操作使用 "! "而不要使用"~"
- typedef unsigned char uint8;
- #define PRINT_BIT_INVERSE 0x11
- #define PRINT_BIT_NOT 0x22
- void uart1_irq() __interrupt 4
- {
- uint8 i;
-
- if (RI) {
- RI = 0;
- i = SBUF;
-
- if (PRINT_BIT_INVERSE == i) { //打印 00 01 或 01 01 結果是不確定的
- uart1_send(IT0);
- IT0 = ~IT0;
- uart1_send(IT0);
- }else if (PRINT_BIT_NOT == i) { //00 01 與 01 00 交替打印,結果與預期一致
- uart1_send(IT0);
- IT0 = !IT0;
- uart1_send(IT0);
- }
- }
復制代碼
2、SDCC 內部數據使用小端存儲,Keil 使用的是大端存儲,所以訪問大于1個字節以上的寄存器時需要分別賦值或讀取
- void uart1_irq() __interrupt 4
- {
- uint8 i;
-
- if (RI) {
- RI = 0;
- i = SBUF;
- uart1_send(i); //先打印執行的指令
-
- //此時 PWM1_CNTR 設置值如下
- //PWM1_CNTRH = 0x12;
- //PWM1_CNTRL = 0x34;
- if (PRINT_PWM1_CNTR == i) { //打印 PWM1_CNTR,打印 34 12
- uart1_send(PWM1_CNTR >> 8);
- uart1_send(PWM1_CNTR);
- }else if (PRINT_PWM1_CNTR_DIV == i) { //分別打印 PWM1_CNTR,打印 12 34
- uart1_send(PWM1_CNTRH);
- uart1_send(PWM1_CNTRL);
- }else if (PRINT_BGV == i) { //打印參考電壓,打印 A4 04
- uart1_send(*BGV >> 8);
- uart1_send(*BGV);
- }else if (PRINT_BGV_DIV == i) { //分別打印參考電壓, 打印 04 A4,0x04A4十進制為1188符合STC8H內部參考電壓范圍
- uart1_send(*BGVH);
- uart1_send(*BGVL);
- }
- }
- }
復制代碼
3、中斷服務聲明或定義必須在main函數所在文件中可見才能正常工作
- //將中斷服務移到 uart1_interrupt.c 文件
- //如果注釋掉下面這個聲明,串口1就不能正常收發
- void uart1_irq() __interrupt 4;
復制代碼
4、char 類型默認為 unsigned char 而非 signed char
- <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">//(編譯前要將首選項中“char類型默認為有符號”選項取消勾選)</p>
- <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">char test = 0; //聲明為 char 或者 unsigned char 時,程序交替打印128個 11 和128個 ff,而不會打印 00</p>
- <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">//signed char test = 0;//聲明為 signed char 時,程序交替打印128個 11 與128個 00 ,而不會打印 ff</p><style type="text/css">p, li { white-space: pre-wrap; }</style>
- void main()
- {
- while (1) {
- WDT_CONTR |= 0x10; //清看門狗
-
- ++test;
- if (test < 0) {
- uart1_send(0x00);
- }else if (test >= 0x80) {
- uart1_send(0xFF);
- }else {
- uart1_send(0x11);
- }
-
- delay_ms(200);
- }
- }
復制代碼 下載:
LitMill-v0.8.7z
(10.91 MB, 下載次數: 13)
2023-8-1 15:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|