久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 290|回復: 9
打印 上一主題 下一主題
收起左側

51單片機驅動CS1237-SO卡死

[復制鏈接]
跳轉到指定樓層
樓主
ID:1145017 發表于 2025-6-13 18:55 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
大佬們幫忙掃一眼, 我們在調試CS1237-SO 時,在READ_CS1237()中卡死,一直沒發現問題出在哪,代碼如下:(代碼參考了站內的24bit ADC CS1237完整驅動源碼,實測可用無bug,只需修改IO口定義 )
#include "stc8h.h"
#include "intrins.h"
#include "stdio.h"
#include "math.h"

typedef unsigned char       u8;
typedef unsigned int       u16;
typedef unsigned long      u32;

//****************** the ADC define

#define ADC_Bit  24          //ADC effect bit, with +/- bit, total 24 bit

#define CS_CON    0X00   //chip regester innter REF output 10HZ PGA=1 chn A 0X00

sbit DOUT = P1^3;//dat output IO
sbit SCLK = P1^0;//sclk IO

u32 result_for_show;

//****************** end of the ADC define

//****************** others defined
bit T0_450MS_flag = 0;

u8 RXT_data = 0;                        // for store the UART receive data
pdata u8 A[5];
//*******************************

void CS1237_INIT();
u32 Read_24BIT_AD();
void Config_CS1237();
u8 Read_chip_Config();
u32 Read_CS1237();
void Config_UART();
void Config_TMR0();
void Delay_ms(u8 ms);
void Delay_us(u8 us);

//*********************** delay function

void Delay_ms(u8 ms)
{
  u8 delay_base;
        
        while(ms--)
        {
                _nop_();
    delay_base = 1350;
                while(delay_base--);
  }
}

void Delay_us(u8 us)
{
  u8 delay_base;
        
        while(us--)
        {
                _nop_();
                _nop_();
    delay_base = 1;
                while(delay_base--);
  }
}

//************************ set the adc


void Config_CS1237(void)        // Config cs1237
{
        u8 i;
        u8 dat;
        u16 count_i=0;                 // timer overflow

        dat = CS_CON;                 // set chip regester 0100 1000
        SCLK = 0;                     //SCK_0;             //sclk pull down
        while(DOUT == 1)                   //chip dat ready for output, sclk == 0, when chip all pull down, means dat ok
        {
                Delay_ms(5);
                count_i++;
                if(count_i > 15000)
                {
                        SCLK = 1;         //SCK_1;
                        DOUT = 1;         //DAT_1;
                        return;           //timer over flow, break
                }
        }
        for(i=0;i<29;i++)// 1 - 29
        {
//                One_CLK;
                SCLK = 1;        //SCK_1;
                Delay_us(48);
                SCLK = 0;        //;SCK_0;
                Delay_us(48);
        }
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);
        //SCK_1;NOP30();DAT_1;SCK_0;NOP30();//30
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//31
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//32
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//33
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//34
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//35
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//36
        
//        One_CLK;            //37     wirted 0x65
        SCLK = 1;Delay_us(48);
        SCLK = 0;Delay_us(48);
        for(i=0;i<8;i++)    // 38 - 45 clk,wirted 8 bit dat
        {
                SCLK = 1;
                Delay_us(48);
                if(dat&0x80)
                        DAT_1;
                else
                        DAT_0;
                dat <<= 1;
                SCLK = 0;
                Delay_us(48);
        }
//        One_CLK;           //46 clk, pull up dat io
          SCLK = 1;Delay_us(48);
          SCLK = 0;Delay_us(48);
}


u8 Read_chip_Config(void)            // reading chip config dat
{
        u8 i;
        u8 dat=0;                  // dat from chip regester
        u16 count_i=0;              // timer cnt
        u8 k=0,j=0;               
        SCK_0;                                //sclk pull down
        while(DOUT == 1)              //chip dat ready for output
        {
                Delay_ms(5);
                count_i++;
                if(count_i > 15000)
                {
                        SCLK = 1;
                        DOUT = 1;
                        return 1;            //timer overflow, break
                }
        }
        for(i=0;i<29;i++)          // 1 - 29
        {
//                One_CLK;
                SCLK = 1;Delay_us(48);
                SCLK = 0;Delay_us(48);
        }
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//30
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//31
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//32
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//33
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//34
        SCLK = 1;Delay_us(36);DOUT = 1;SCLK = 0;Delay_us(36);//35
        SCLK = 1;Delay_us(36);DOUT = 0;SCLK = 0;Delay_us(36);//36
        DOUT = 1;
        
//        One_CLK;            //37     wirted 0x56
        SCLK = 1;Delay_us(48);
        SCLK = 0;Delay_us(48);
        dat=0;
        for(i=0;i<8;i++)    // 38 - 45 clk, reading dat
        {
//                One_CLK;
                SCLK = 1;Delay_us(48);
                SCLK = 0;Delay_us(48);
                dat <<= 1;
                if(DOUT == 1)
                        dat++;
        }
//        One_CLK;          //46 clk, pull up dat io
        SCLK = 1;Delay_us(48);
        SCLK = 0;Delay_us(48);
        return dat;
}


u32 Read_CS1237(void)     // reading chip, return +/- dat
{
        u8 i;
        u32 dat=0;              // reading dat
        u8 count_i=0;          // timer cnt
        DOUT = 1;                         // dat io lock 1, 51 must be do
        SCLK = 0;                            // timer pull dowm
        while(DOUT == 1)                       // dat ready for output
        {
                Delay_ms(5);
                count_i++;
                if(count_i > 300)
                {
                        SCLK = 1;
                        DOUT = 1;
                        return 0;                     // timer overflow, break
                }
        }
        DOUT = 1;                        //dat io lock 1,51 must be do
        dat=0;
        for(i=0;i<24;i++)               // getting 24 bit dat
        {
                SCLK = 1;
                Delay_us(48);
                dat <<= 1;
                if(DOUT == 1)
                        dat ++;
                SCLK = 0;
                Delay_us(48);        
        }
        for(i=0;i<3;i++)           // 27 clk input in all
        {
//                One_CLK;
                SCLK = 1;Delay_us(48);
                SCLK = 0;Delay_us(48);
        }
//        DAT_1;
               
        i = 24 - ADC_Bit;       //i is the to be delete bit
        dat >>= i;            
        

        return dat;
}


//***********************config the uart 1#

void Config_UART()
{        
        SCON = 0X50;            // uart 1 working in 8 bit model, baud changeable Bin 0101 0000
        
        T2L = 0XE0;              // the baud rate is 9600, main fosc @ 11.0592
        T2H = 0XFE;
        
        AUXR |= 0X01;           // set tmr2 as the uart 1# baud generator    Bin 0000 0001
        AUXR |= 0X04;           // set tmr2 as 1t model Bin 0000 0100
        AUXR |= 0X10;           // start the T2   Bin 0001 0000
        
        ES = 1;                 // enable the interrupt
}

//*********************** the uart 1# send bit data

void Uart_1_Send_data(u8 dat)
{
  SBUF = dat;
        while(!TI);
        TI = 0;
}


//************************the uart 1# send string

void Uart_1_Send_string(char *s)
{
  while(*s)
        {Uart_1_Send_data(*s++);}
}


//**********************config the tmr0

void Config_TMR0()
{
  AUXR |= 0X80;
        TMOD &= 0XF0;
        
        TL0 = 0XCD;         // 1 ms for timer T0 16BIT AUTO LOAD MODEL @T 11.0592  HZ
        TH0 = 0XD4;
        
        TF0 = 0X00;
        ET0 = 0X01;              
        PT0 = 0X01;           // set the t0 interrupt as the 1st one
        TR0 = 0X01;           // start the T0
        ET0 = 0X01;      
}


//************************* the main

void main()
{        

        P0M0 = ~0XFF;            // P1^0, P1^1 as open drain model,  others as pull-push model  
        P0M1 &= 0X00;
        
        P1M0 = (P1M0 & ~0x04) | 0x01;
        P1M1 &= ~0x05;

        
        P2M0 = ~0XFF;            // P2 as pull-push model  BIN 1111 1111
        P2M1 &= 0X00;            //M1 = 0, M0 = 1 is pull-push model
        
        P3M0 = ~0XFF;            // P3 as pull-push model  BIN 1111 1111
        P3M1 &= 0X00;            //M1 = 0, M0 = 1 is pull-push model
        
        P4M0 = ~0XFF;            // P4 as pull-push model  BIN 1111 1111
        P4M1 &= 0X00;            //M1 = 0, M0 = 1 is pull-push model
        
  P5M0 = ~0XFF;            // P5 as pull-push model  BIN 1111 1111
        P5M1 &= 0X00;            //M1 = 0, M0 = 1 is pull-push model

  Config_UART();
  Config_TMR0();        
  Config_CS1237();
  Read_chip_Config();
  Read_CS1237();
  CS1237_INIT();


       ET0 = 1;
        EA  = 1;
        
        P1 = 0XFF;
        
        Delay_ms(1);
        
        while(1)
        {
        
                if(T0_450MS_flag == 1)
                {
      T0_450MS_flag = 0;               
                        Uart_1_Send_string("Start the ADC process\r\n");         
        

                        result_for_show = Read_CS1237();   

                        sprintf(A, "%d\r\n", result_for_show);
                        Uart_1_Send_string(A);
                 
      }        
     }
                }

// ************* the tmr0 interrupt  450ms for 1 time adc scan

void TMR0_isr() interrupt 1
{
  static u16 tmr0_cnt = 0;
        tmr0_cnt++;
        if(tmr0_cnt >= 500)
        {
    tmr0_cnt   = 0;
                T0_450MS_flag = 1;
  }
}

//************* the uart 1# interupt

void UART_1_isr() interrupt 4
{
  if(RI)
        {
    RI       = 0;
                SBUF     = SBUF;
                RXT_data = SBUF;
  }
        if(TI)
        { TI       = 0; }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:57657 發表于 2025-6-13 20:56 | 只看該作者
u8 count_i=0;    范圍:0~255 然后下面是
if(count_i > 300) 條件能成立就是見鬼了。
回復

使用道具 舉報

板凳
ID:1034262 發表于 2025-6-13 21:16 | 只看該作者
CS1237接口時序簡單,怎么能卡死?訪問又不用等待。
回復

使用道具 舉報

地板
ID:1023753 發表于 2025-6-14 09:15 | 只看該作者
接口模式不對,時鐘可以是推完模式,數據接口應該是準雙向口
回復

使用道具 舉報

5#
ID:1145017 發表于 2025-6-14 09:23 | 只看該作者
u8 count_i = 0, 已修正位 u16 count_i,  代碼可以跑, 就是讀不到數,(DOUT = 準雙向IO, SCLK = 推挽)
示波器看DOUT 波形,發現高低電平不明顯如圖 , 請問大佬這是什么原因
回復

使用道具 舉報

6#
ID:584814 發表于 2025-6-16 09:48 | 只看該作者
看著滿篇英文注釋感覺不明覺厲的樣子。
坐等結果學習
回復

使用道具 舉報

7#
ID:1145017 發表于 2025-6-16 15:35 | 只看該作者
是的, SCLK 是推挽模式, DOUT 是準雙向口。
回復

使用道具 舉報

8#
ID:1154227 發表于 2025-6-16 20:47 | 只看該作者
i的范圍不對啊,255最多了,根本到不了300
回復

使用道具 舉報

9#
ID:1130105 發表于 2025-6-17 08:45 | 只看該作者
dat <<= 1;<b1204> if(DOUT == 1)
dat ++;
里面的    <b1204>    是什么意思?宏定義嗎?也沒看到#define的位置啊。
回復

使用道具 舉報

10#
ID:1145017 發表于 2025-6-17 18:58 | 只看該作者

您好, 你指的《B1204》在哪個位置, 代碼中沒有這個《B1204》
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美国产亚洲一区二区 | 精品欧美一区二区在线观看 | 黄色片在线免费看 | 国产精品欧美一区喷水 | 日韩免费成人av | 亚洲一区二区在线 | 成人在线精品视频 | 亚洲图片一区二区三区 | 精品久久电影 | 视频一区二区三区在线观看 | 黄色网页在线 | 欧美一区二区三区在线观看视频 | 久久久久国产精品午夜一区 | 欧美精品久久久 | www成年人视频 | 欧美成人精品一区二区三区 | 久久99精品国产自在现线小黄鸭 | 日韩精品一区二区三区视频播放 | 九色91视频 | 一区二区三区国产好 | 懂色av一区二区三区在线播放 | 天天天天操 | 自拍视频网站 | 天天摸天天看 | 99久久婷婷国产亚洲终合精品 | 国产精品久久久久久久久久免费看 | 中文字幕在线第一页 | 亚洲日日| 91在线精品播放 | 精品美女在线观看视频在线观看 | 一区二区不卡视频 | 久久亚洲综合 | 韩日免费视频 | 色婷婷综合网 | 国产精品免费看 | 99在线观看 | 777zyz色资源站在线观看 | 亚洲精品久久久蜜桃 | 精品国产视频 | 超碰最新在线 | 国产在线一区观看 |