• 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: 接收串列傳輸數據
#7
高級會員
高級會員


查看用戶資訊
謝謝大佬,確實是中斷互相蓋過導致。
我將一些不用即時處理的程式移到主迴路就可以了。
謝謝各位大佬的指導

發表於: 2022/4/15 16:51
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 接收串列傳輸數據
#6
管理員
管理員


查看用戶資訊
a780917a你好,請問User_RA2_ISR()和User_RA4_ISR()皆由中斷來觸發嗎?
如果是,建議可以修改為以下程式再試試:

void User_RA2_ISR(void)
{
if(IO_RA4_PORT)
{
cn = 1;
}
else cn = 0;
if(j < 23){
Alldata = Alldata | ( (cn & 0x01) << j );
j++;
}
}

猜測可能是User_RA4_ISR()未觸發前User_RA2_ISR()再次執行,造成最後一位錯誤。
也有可能是User_RA2_ISR()第一次抓取的資料與預期的起始點不同,可以在程式中加入判斷來確保抓取資料的時機正確。

發表於: 2022/4/14 9:35
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 接收串列傳輸數據
#5
高級會員
高級會員


查看用戶資訊
謝謝大佬,儲存數據這部分OK了。

我發現我抓取的Data會亂跳,但是波形在視波器顯示都滿穩的
SLK與SDA 我用RA2做SLK引腳,IOC上緣觸發中斷,而RA4上緣觸發則是在RA2為於高位時數值重新指向低位。用ICD3 Debug監測每一RUN的數值都不一樣= =

void User_RA2_ISR(void)
{
if(IO_RA4_PORT)
{
cn = 1;
}
else cn = 0;
Alldata = Alldata | ( (cn & 0x01) << j );
if(j < 23)j++;
}
void User_RA4_ISR(void)
{
if(IO_RA2_PORT && j == 23 )
{
j = 0;
Value.AllMsg = (0x0fff & Alldata);
FlagReg1.Minus_FLAG = ((Alldata >> 20) & 0x01);
Alldata = 0;
}
}

發表於: 2022/4/13 14:40
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 接收串列傳輸數據
#4
資深會員
資深會員


查看用戶資訊
struct _BITS_32
{
unsigned char bit00: 1;
unsigned char bit01: 1;
unsigned char bit02: 1;
unsigned char bit03: 1;
unsigned char bit04: 1;
unsigned char bit05: 1;
unsigned char bit06: 1;
unsigned char bit07: 1;
unsigned char bit08: 1;
unsigned char bit09: 1;
unsigned char bit10: 1;
unsigned char bit11: 1;
unsigned char bit12: 1;
unsigned char bit13: 1;
unsigned char bit14: 1;
unsigned char bit15: 1;
unsigned char bit16: 1;
unsigned char bit17: 1;
unsigned char bit18: 1;
unsigned char bit19: 1;
unsigned char bit20: 1;
unsigned char bit21: 1;
unsigned char bit22: 1;
unsigned char bit23: 1;
unsigned char reserved: 8;

};

typedef union
{
unsigned long AllMsg;
struct _BITS_32 bits;
}BITS_32;

BITS_32 tb;
void test(void)
{
unsigned long Alldata;
unsigned char i;
unsigned char cn;//收到的資料

//用這樣收集資料聚集起來,從hi bit開始收
for(i=0;i<23;i++)
{
Alldata = (Alldata << 1) | (cn & 0x01);
}

//用這樣收集資料聚集起來,從low bit開始收
for(i=0;i<23;i++)
{
Alldata = Alldata | ( (cn & 0x01) << i );
}

//最後如果要讀取一個bit的資料
tb.AllMsg = Alldata;
cn = tb.bits.bit00 ; //類似這樣就可以讀取bit0資料
}

以上請參考

發表於: 2022/4/13 10:13
除役的胖子FAE
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 接收串列傳輸數據
#3
高級會員
高級會員


查看用戶資訊
對,我想問Bit排列寫入的方法,是類似下面這樣,把數值依序放入a0~a7這樣
ByteValue1就可以直接提取16進制的數值嗎?
struct ByteValue1
{
unsigned a0 : 1 ;
unsigned a1 : 1 ;
unsigned a2 : 1 ;
unsigned a3 : 1 ;
unsigned a4 : 1 ;
unsigned a5 : 1 ;
unsigned a6 : 1 ;
unsigned a7 : 1 ;
};
另一個問題是我想用每次CLK觸發時,用迴圈填入數據,但是bit形式的陣列是怎麼宣告呢?
if(j<23)
{a[ j ] = Data;}
else
j = 0;
沒找到這類範例的關鍵字,厚顏來請各位老哥提點:D

發表於: 2022/4/12 16:11
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 接收串列傳輸數據
#2
資深會員
資深會員


查看用戶資訊
24bit = 3bytes, 那不就是用3bytes存就好,何需char tb[23] 這個23bytes的陣列呢?還是我有誤會?

發表於: 2022/4/12 15:02
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


接收串列傳輸數據
#1
高級會員
高級會員


查看用戶資訊
各位好, 小弟想用PIC16LF1823接收一組24個bit的串列數據,數據是一個20bit的數值,後4位是符號位。
目前是用char tb[23]陣列儲存起來,但要加總時就變得相當麻煩,想問看看該怎麼簡化呢?

發表於: 2022/4/12 10:04
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... ]

教育訓練中心

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