找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 154|回复: 5
打印 上一主题 下一主题
收起左侧

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:584814 发表于 2025-6-16 09:48 | 只看该作者
看着满篇英文注释感觉不明觉厉的样子。
坐等结果学习
回复

使用道具 举报

6#
ID:1154227 发表于 2025-6-16 20:47 | 只看该作者
i的范围不对啊,255最多了,根本到不了300
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表