• slider image 442
  • slider image 477
  • slider image 479
  • slider image 480
  • slider image 481
  • slider image 482
:::


Browsing this Thread:   1 Anonymous Users






Re: UART傳輸與接收問題
#4
高級會員
高級會員


查看用戶資訊
感謝Dark大跟版大的回覆
真的是TX程式的問題,多加一行while(!TXSTAbits.TRMT);,問題就改善了。
又學到一課,太棒了

發表於: 2021/2/8 8:24
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: UART傳輸與接收問題
#3
版主
版主


查看用戶資訊
看了你的十進制及 ASCII Code 的轉換覺得怪怪的。
參照:

Speed_V = Speed_Command;
uart_tb[0] = 0xA5;
uart_tb[1] = (Speed_V/100) +'0';
uart_tb[2] = ((Speed_V/10)%10) +'0';
uart_tb[3] = (Speed_V%10)+'0';
uart_tb[4] = Er_F +'0';
uart_tb[5] = 0x0D;

建議你將這段轉換實際跑一下,利用斷點方式檢查一下轉換後的 uart_tb[n] 的值為何?

參考一下底下將 16 進制轉成 10 十進制的顯示資料,自己再加上 0x30 變成可顯示的 ASCII 的數字:
DisplayDigit3((CSM_ever_Freq/100)); //將 16 進制的值先除以 100 得到百位數。 顯示百位數
CSM_ever_Freq=CSM_ever_Freq%100; // 計算剩下的餘數值,並回填回去

DisplayDigit4(CSM_ever_Freq/10); // 計算十位數
CSM_ever_Freq=CSM_ever_Freq%10; // 計算餘數找出剩下的個位數

DisplayDigit5(CSM_ever_Freq); // 顯示個位數

發表於: 2021/2/4 15:03
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: UART傳輸與接收問題
#2
資深會員
資深會員


查看用戶資訊
EUSART_Write(uart_tb[k]);
這行下面需多一個檢查TXREG是否清空
while(!TXSTAbits.TRMT);
你沒檢查發送狀態就是會被後面的蓋過去。
而且你TX程式也很怪
應該是要
while(1)
{
        
Speed_V Speed_Command;
        
uart_tb[0] = 0xA5;
        
uart_tb[1] = (Speed_V/100) +'0';
        
uart_tb[2] = ((Speed_V/10)%10) +'0';
        
uart_tb[3] = (Speed_V%10)+'0';
        
uart_tb[4] = Er_F +'0';
        
uart_tb[5] = 0x0D;
        for(
uint8_t i=0;i<6;i++)
        {
                 while(!
TXSTAbits.TRMT); //check txreg is emtpy
                 
EUSART_Write(uart_tb[i]); //write data to txreg
        
}
}

發表於: 2021/2/3 22:41
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


UART傳輸與接收問題
#1
高級會員
高級會員


查看用戶資訊
各位好:
我使用兩個PIC16F1823要做UART傳輸,丟一筆三位數的資料, 並用七段顯示器顯示數值
我有把百位,十位,各位,拆開來用ASCll傳輸
我遇到的問題是, 當數值從299 要跳到300時 有時數值會偶爾出現 200 或399, 感覺只有其中一兩個位數先變化
尤其在有干擾源的情況下狀況更嚴重,
我已經在接收端增加許多限制避免漏收就讀出,但狀況依然沒有完全改善
9600 8bit
TX:(放在主程式的While迴圈)
while(1)
{
Speed_V = Speed_Command;
uart_tb[0] = 0xA5;
uart_tb[1] = (Speed_V/100) +'0';
uart_tb[2] = ((Speed_V/10)%10) +'0';
uart_tb[3] = (Speed_V%10)+'0';
uart_tb[4] = Er_F +'0';
uart_tb[5] = 0x0D;
EUSART_Write(uart_tb[k]);
if(k++ == 5)
{
//Speed;
k = 0;
}
}

RX:(用中斷接收)
void User_UART_ISR(void)
{
while (EUSART_DataReady)
{
if(FlagReg1.Uart_FLAG == 0)
{
value = EUSART_Read();
if(value = 0xA5)
{
FlagReg1.Uart_FLAG = 1;
k = 0;
}
}
else
{
value = EUSART_Read();
uart_tb[k] = value;
k++;
if(k==5 && value == 0x0D)
{
if(uart_tb[0] >= 0x30 && uart_tb[1] >= 0x30 && uart_tb[2] >= 0x30)
{
Speed_S = (uart_tb[0]-'0') *100 + (uart_tb[1]-'0') *10 + (uart_tb[2]-'0');
Speed_Value = Speed_S * 1;
}
uart_tb[0] = 0;
uart_tb[1] = 0;
uart_tb[2] = 0;
FlagReg1.Uart_FLAG = 0;
}
}
}
}

發表於: 2021/2/2 11:03
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------