SOGO論壇
  登入   註冊   找回密碼
查看: 2167|回覆: 6
列印 上一主題 下一主題

[問題求助] 各位大大請幫幫忙 8051-RS232-ADC0804 [複製連結]

Rank: 2

狀態︰ 離線
跳轉到指定樓層
1
發表於 2011-11-20 18:06:24 |只看該作者 |倒序瀏覽 | x 1
本帖最後由 sss555 於 2011-11-26 21:50 編輯

跪求各位大大~現在我這邊有兩個程式 首先我將8051+ADC0804 此程式能動作,實做出來也能動~再來8051+RS232(但是這輸出只用4位元如果可以的話能幫忙讓它輸出8位元)此程式能動作,實做出來也能動,問題來了我將這兩個程式合在一起卻沒功能了,可否有大師幫幫忙,小弟已經兩個多禮拜了~都在耗在此程式上了~望請各位大大幫幫忙。附上程式
8051-ADC0804
#include<reg51.h>
sbit wr= P2^0;        // Write pin. It is used to start the conversion.
sbit rd= P2^1;        // Read pin. It is used to extract the data from internal register to the output pins of ADC.
sbit intr= P2^2;        // Interrupt pin. This is used to indicate the end of conversion. It goes low when conversion is complete.

void transmit()        // Function for serial tranmission of data.
{
    SBUF=P1;
    while(TI==0);
    TI=0;
}

void delay(unsigned int msec )        // The delay function provides delay in msec.
{
    int i ,j ;
    for(i=0;i<msec;i++)
        for(j=0; j<1275; j++);
}

void adc()        //Function to read the values from ADC and tranmit serially.
{
    rd=1;
    wr=0;
    delay(2);
    wr=1;
    while(intr==1);
    rd=0;
    transmit();
    delay(2);
    intr=1;
}

void init()        // Intialize timer 1 in mode 2 for serial transmission. The baud rate is set to 9600bps.
{
    TMOD=0x20;
    TH1=0xFD;
    SCON=0x50;
    TR1=1;
}

void main()
{
    P1=0xff;        // Declare P1 as input port.
    init();
    while(1)
    {
        adc();
    }
}

喜歡嗎?分享這篇文章給親朋好友︰
               感謝作者     

Rank: 3Rank: 3

數位軟體勳章

狀態︰ 離線
2
發表於 2011-11-20 20:40:05 |只看該作者
呼 我也不會說/////   不過

我有幫您整理一下 Code 不然好長阿....  
  1.         #include "reg51.h"        /*載入標頭檔*/
  2.         sbit wr= P2^0;                        // Write pin. It is used to start the conversion.
  3.         sbit rd= P2^1;                        // Read pin. It is used to extract the data from internal register to the output pins of ADC.
  4.         sbit intr= P2^2;                // Interrupt pin. This is used to indicate the end of conversion. It goes low when conversion is complete.

  5.         char signal=0xff;
  6.         char a1=0x7f;       
  7.         char x=0xff;
  8.         int  w=0;
  9.         int  t=0;
  10.        
  11.         char q[11]        ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; /*存放硬體電路資料陣列*/
  12.         char o[10]        ={0x0a,0x09,0x01,0x01,0x0a,0x07,0x08,0x06,0x03,0x08};      /*0,9,1,1,0,7,8,6,3,8*/
  13.         char o1[10]        ={0x0a,0x04,0x02,0x05,0x02,0x0a,0x01,0x07,0x02,0x08};      /*0425201728*/
  14.        
  15.        
  16.         void init()        
  17.         {
  18.                 // Intialize timer 1 in mode 2 for serial transmission. The baud rate is set to 9600bps.
  19.                 TMOD=0x20;
  20.                 TH1=0xFD;
  21.                 SCON=0x50;
  22.                 TR1=1;
  23.         }
  24.        
  25.         void transmit()        
  26.         {
  27.                 //Function for serial tranmission of data.
  28.                 SBUF=P1;
  29.                 while(TI==0);
  30.                 TI=0;
  31.         }
  32.        
  33.         void delay(unsigned int msec )        
  34.         {
  35.                 // The delay function provides delay in msec.
  36.           int i ,j ;
  37.           for(i=0;i<msec;i++)
  38.            for(j=0; j<1275; j++);
  39.         }

  40.         void EX0_int(void) interrupt 0                  
  41.         {
  42.                 /*INT0中斷程式碼*/
  43.                 char a=0xff;
  44.                 char b=0x0f;
  45.        
  46.                 a=b&P1;
  47.                 x=a;
  48.                 t=1;
  49.         }
  50.        
  51.         void SCON_int(void) interrupt 4                          
  52.         {
  53.                 /*串列埠程式碼*/
  54.                 if(TI==1)
  55.                 {       
  56.                   TI=0;
  57.           }
  58.                 else
  59.                 {
  60.                   RI=0;
  61.                 }
  62.         }       
  63.        
  64.         void adc()        
  65.         {
  66.                 //Function to read the values from ADC and tranmit serially.
  67.           rd=1;
  68.           wr=0;
  69.           delay(2);
  70.           wr=1;
  71.           while(intr==1);
  72.           rd=0;
  73.           transmit();
  74.           delay(2);
  75.           intr=1;
  76.         }
  77.                
  78.         void main()
  79.         {      
  80.                 P1          =        0xff;        // Declare P1 as input port.
  81.                 init();
  82.          
  83.                 TMOD        =        0x20;        /*設定為timer1計時計數*/
  84.                 TH1          =        0xfd; /*9600*/
  85.                 TR1          =        1;  
  86.                 SCON        =        0x50;        /*串列埠設定為mode1*/
  87.        
  88.                 IE          =        0x91;        /*致能中斷暫存器 外部中斷0控制*/
  89.                 TCON        = 0x41;        /*外部中斷 負緣觸發動作啟動timer1*/
  90.                
  91.                 while(1)                       
  92.                 {
  93.                         /*程式開始*/                       
  94.                         adc();
  95.                        
  96.                         P2        =        signal;
  97.                         if(t==1)
  98.                         {
  99.                                 if(w<=10)
  100.                                 {
  101.                                         q[w]        =        x;
  102.                                         SBUF        =        x;      
  103.                                         w        =        w        +        1;
  104.                                 }
  105.                                 t        =        t        +        1;               
  106.                         }
  107.                                   
  108.                         if(t==2)                                
  109.                         {
  110.                                 /*判斷號碼程式*/
  111.                                 if(q[1]==o[0])
  112.                                         if(q[2]==o[1])
  113.                                                 if(q[3]==o[2])
  114.                                                         if(q[4]==o[3])
  115.                                                                 if(q[5]==o[4])
  116.                                                                         if(q[6]==o[5])
  117.                                                                                 if(q[7]==o[6])
  118.                                                                                         if(q[8]==o[7])                  
  119.                                                                                                 if(q[9]==o[8])
  120.                                                                                                         if(q[10]==o[9])
  121.                                                                                                         {                               
  122.                                                                                                                 signal=a1;
  123.                                                                                                                 P2=signal;
  124.                                                                                                                 t=0;                                                                                               
  125.                                                                                                         }
  126.                                                                                                         else
  127.                                                                                                         {
  128.                                                                                                                 t=t+1;
  129.                                                                                                         }
  130.                                                                                                 else
  131.                                                                                                 {
  132.                                                                                                         t=t+1;
  133.                                                                                                 }
  134.                                                                                         else
  135.                                                                                         {
  136.                                                                                                 t=t+1;
  137.                                                                                         }
  138.                                                                                 else
  139.                                                                                 {
  140.                                                                                         t=t+1;
  141.                                                                                 }
  142.                                                                         else
  143.                                                                         {
  144.                                                                                 t=t+1;                                                               
  145.                                                                         }
  146.                                                                 else
  147.                                                                 {
  148.                                                                         t=t+1;
  149.                                                                 }
  150.                                                         else
  151.                                                         {
  152.                                                                 t=t+1;
  153.                                                         }
  154.                                                 else
  155.                                                 {
  156.                                                         t=t+1;
  157.                                                 }
  158.                                         else
  159.                                         {
  160.                                                 t=t+1;
  161.                                         }
  162.                                 else
  163.                                 {
  164.                                         t=t+1;
  165.                                 }
  166.                         }
  167.             if(t==3)
  168.             {
  169.                     if(q[1]==o1[0])
  170.                       if(q[2]==o1[1])
  171.                         if(q[3]==o1[2])
  172.                           if(q[4]==o1[3])
  173.                             if(q[5]==o1[4])
  174.                                      if(q[6]==o1[5])
  175.                                 if(q[7]==o1[6])
  176.                                   if(q[8]==o1[7])
  177.                                     if(q[9]==o1[8])
  178.                                       if(q[10]==o1[9])
  179.                                 {
  180.                                         signal        =        a1;
  181.                                   P2        =        signal;
  182.                                   t=0;
  183.                                 }
  184.                                 else
  185.                                 {
  186.                                         t=0;
  187.                                 }
  188.                               else
  189.                               {
  190.                                       t=0;
  191.                               }
  192.                             else
  193.                             {
  194.                               t=0;
  195.                             }
  196.                           else
  197.                           {
  198.                             t=0;
  199.                           }
  200.                         else
  201.                         {
  202.                           t=0;
  203.                         }
  204.                       else
  205.                       {
  206.                               t=0;
  207.                       }
  208.                     else
  209.                     {
  210.                             t=0;
  211.                     }
  212.                   else
  213.                   {
  214.                     t=0;
  215.                   }
  216.                 else
  217.                 {
  218.                   t=0;
  219.                 }
  220.               else
  221.               {
  222.                 t=0;
  223.               }
  224.             }
  225.           }
  226.         }
  227.        
複製代碼
已有 1 人評分SOGO幣 收起 理由
又靜 + 5 感謝您熱心幫助會員,論壇需要您的繼續支持.

總評分: SOGO幣 + 5   查看全部評分

感同身受,就能放下執著

SOGO榮譽會員

↑↑雖小郎↑↑

Rank: 13Rank: 13Rank: 13Rank: 13

榮譽會員勳章 原創及親傳圖影片高手勳章 熱心參予論壇活動及用心回覆主題勳章

狀態︰ 離線
3
發表於 2011-11-21 09:16:40 |只看該作者
我也學樓上,承認我也不會
但是請問您是怎樣燒兩隻程式進同一片晶片的呢?
會不會是因為有兩個main()所以不會跑呢
我已經承認我不會了
所以不要打我~~
已有 1 人評分威望 收起 理由
又靜 + 1 夜香兄,幾時要教教小妹 ^^

總評分: 威望 + 1   查看全部評分

Rank: 5Rank: 5

數位軟體勳章

狀態︰ 離線
4
發表於 2011-11-21 09:28:48 |只看該作者
你有沒有原理圖.否則無法得知你程式如何工作.另外就如 "記得此夜香"所講同一個程式不應該有兩個同名的函式(function name) 更何況是程式進入點(main).再者你也沒呼叫 ADC這個function.他怎麼會動作

除非你的ADC一值傳出base line value

點評

記得此夜香  不用抱歉啦,您肯出來說話提出觀點,就代表了您有想幫他人的心,光這個就很難得了,更何況兩隻程式放的那麼靠近,我一開始也看錯  發表於 2011-11-21 16:57:17
已有 1 人評分威望 收起 理由
又靜 + 1 感謝您熱心幫助會員解決問題,論壇需要您的.

總評分: 威望 + 1   查看全部評分

失敗只有一種:那就是半途而廢

Rank: 5Rank: 5

數位硬體勳章

狀態︰ 離線
5
發表於 2011-11-21 12:05:10 |只看該作者
本帖最後由 Jackchen00 於 2011-11-21 18:53 編輯

你的程式除了樓上大大的回覆之外
我想跟你分享ㄧ些觀念
如果你的變數沒有正負號話
最好是宣告為unsigned char(8bit)或unsigned int(16bit)
主程式main()只能有一個

如果你的這兩行
char o[10]        ={0x0a,0x09,0x01,0x01,0x0a,0x07,0x08,0x06,0x03,0x08};      /*0,9,1,1,0,7,8,6,3,8*/

char o1[10]        ={0x0a,0x04,0x02,0x05,0x02,0x0a,0x01,0x07,0x02,0x08};      /*0425201728*/
只是一個常數不會變動的話
你應該宣告成
unsigned char code o[10]={0x0a,0x09,0x01,0x01,0x0a,0x07,0x08,0x06,0x03,0x08};      /*0,9,1,1,0,7,8,6,3,8*/
unsigned char code o1[10]={0x0a,0x04,0x02,0x05,0x02,0x0a,0x01,0x07,0x02,0x08};      /*0425201728*/
放在程式碼裡面才不會佔用資料記憶體(暫存器)

還有用RS232做傳輸最好還是加上Parity check及Checksum以避免傳輸中發生錯誤

你的部分主程式我幫你改成以下的程式
        while(1)           /*程式開始*/
         {
                           P2=signal;
                           if(t==1)
                                 {
                                          if(w<=10)
                                          {
                                         q[w]=x;
                                         SBUF=x;        
                                         w++;
                                         }
                                  t=2;               
                                }
                           
                           else if(t==2)                                /*判斷號碼程式*/
                                   {
                                     if (((q[1]==o[0])  && (q[2]==o[1]) && (q[3]==o[2]) && (q[4]==o[3])
                                          && (q[5]==o[4]) && (q[6]==o[5]) && (q[7]==o[6]) && (q[8]==o[7])                  
                                          && (q[9]==o[8]) && (q[10]==o[9])) ||
                                          ((q[1]==o1[0]) && (q[2]==o1[1]) && (q[3]==o1[2]) && (q[4]==o1[3])
                                          && (q[5]==o1[4]) && (q[6]==o1[5]) && (q[7]==o1[6]) && (q[8]==o1[7])
                                          && (q[9]==o1[8]) && (q[10]==o1[9])))
                                     {
                                         signal=a1;
                                         P2=signal;
                                     }
                                         t=0;
                                        }
         }

另外
void EX0_int(void) interrupt 0                  /*INT0中斷程式碼*/
{
char a=0xff;
char b=0x0f;

a=b&P1;
x=a;
t=1;
}

這段程式中
1,   a不需設初值,設初值會增加一個指令
2,   不需設a,b這個變數,只要將a=b&P1改成x = P1 & 0x0f;即可

EX0_int() interrupt 0                  /*INT0中斷程式碼*/
{
   x = P1 & 0x0f;
   t = 1;
}


還有你的變數w在大於10以後並沒有做歸零的動作
                                          if(w<=10)
                                          {
                                         q[w]=x;
                                         SBUF=x;        
                                         w++;
                                         }
你的這段程式可能會有問題
已有 1 人評分威望 收起 理由
又靜 + 2 感謝您熱心幫助會員解決問題,論壇需要您的.

總評分: 威望 + 2   查看全部評分

SOGO榮譽會員

↑↑雖小郎↑↑

Rank: 13Rank: 13Rank: 13Rank: 13

榮譽會員勳章 原創及親傳圖影片高手勳章 熱心參予論壇活動及用心回覆主題勳章

狀態︰ 離線
6
發表於 2011-11-21 12:19:01 |只看該作者
本帖最後由 記得此夜香 於 2011-11-21 12:30 編輯
alphi 發表於 2011-11-21 09:28  
你有沒有原理圖.否則無法得知你程式如何工作.另外就如 "記得此夜香"所講同一個程式不應該有兩個同名的函式( ...


void main()
{
    P1=0xff;        // Declare P1 as input port.
    init();
    while(1)
    {
        adc();
    }
}


好像有呼叫到

另外支持大大所說的
應該要附上想要用這三個晶片做什麼
不然怎知道怎樣改才對

點評

alphi  抱歉,我只看到最大的main() 裡面沒有呼叫到ADC  發表於 2011-11-21 16:33:14

Rank: 2

狀態︰ 離線
7
發表於 2011-11-21 17:39:56 |只看該作者
本帖最後由 sss555 於 2011-11-21 17:40 編輯

附上電路圖,謝謝各位的幫忙
他的程式碼
// Program to read the values of ADC and serially tranmitting it to PC.
#include<reg51.h>
sbit wr= P2^0;        // Write pin. It is used to start the conversion.
sbit rd= P2^1;        // Read pin. It is used to extract the data from internal register to the output pins of ADC.
sbit intr= P2^2;        // Interrupt pin. This is used to indicate the end of conversion. It goes low when conversion is complete.

void transmit()        // Function for serial tranmission of data.
{
    SBUF=P1;
    while(TI==0);
    TI=0;
}

void delay(unsigned int msec )        // The delay function provides delay in msec.
{
    int i ,j ;
    for(i=0;i<msec;i++)
        for(j=0; j<1275; j++);
}

void adc()        //Function to read the values from ADC and tranmit serially.
{
    rd=1;
    wr=0;
    delay(2);
    wr=1;
    while(intr==1);
    rd=0;
    transmit();
    delay(2);
    intr=1;
}

void init()        // Intialize timer 1 in mode 2 for serial transmission. The baud rate is set to 9600bps.
{
    TMOD=0x20;
    TH1=0xFD;
    SCON=0x50;
    TR1=1;
}

void main()
{
    P1=0xff;        // Declare P1 as input port.
    init();
    while(1)
    {
        adc();
    }
}

8051-adc0804-rs232.gif (39.84 KB, 下載次數: 27)

請注意︰利用多帳號發表自問自答的業配文置入性行銷廣告者,將直接禁訪或刪除帳號及全部文章!
您需要登錄後才可以回覆 登入 | 註冊


本論壇為非營利自由討論平台,所有個人言論不代表本站立場。文章內容如有涉及侵權,請通知管理人員,將立即刪除相關文章資料。侵權申訴或移除要求:abuse@oursogo.com

GMT+8, 2025-3-14 06:32

© 2004-2025 SOGO論壇 OURSOGO.COM
回頂部