最近用到CS1237的,用GD32F103驱动的,代码是参照其他人的改写的。
CS1237的时序驱动是有要求的,这个代码把底层IO分开,设配其它单片机时候,
只要改下函数就行了
void CS1237_Data_IS_Out_HIGH();
void CS1237_Data_IS_Out_LOW();
void CS1237_Data_IS_INPUT();
void CS1237_Data_IS_INPUT_LOW();
void CS1237_SCLK_HIHG();
void CS1237_SCLK_LOW();
目前测试结果ok的,发出来给大家参考下
/*
* Copyright (c) 2006-2022, yang Team
*
* SPDX-License-Identifier: NONE
*
*Change Logs:
* Date Author Notes
* 2022-xx-xx linchenuyang first implementation
*/
#include "cs1237.h"
#include "stm32f10x.h"
#include "delay.h"
#define CS1237_Data_PORT GPIOB
#define CS1237_Data_PIN GPIO_Pin_14
#define CS1237_Data_PIN_STATE() GPIO_ReadInputDataBit(CS1237_Data_PORT,CS1237_Data_PIN)
#define CS1237_CLK_PIN GPIO_Pin_15
#define CS1237_CLK_PORT GPIOB
//*************************************************************************************************************
void CS1237_Data_IS_Out_HIGH()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_Data_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_Data_PORT, &GPIO_InitStructure);
GPIO_SetBits(CS1237_Data_PORT,CS1237_Data_PIN);
}
//*************************************************************************************************************
void CS1237_Data_IS_Out_LOW()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_Data_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_Data_PORT, &GPIO_InitStructure);
GPIO_ResetBits(CS1237_Data_PORT,CS1237_Data_PIN);
}
//*************************************************************************************************************
void CS1237_Data_IS_INPUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_Data_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_Data_PORT, &GPIO_InitStructure);
GPIO_ResetBits(CS1237_Data_PORT,CS1237_Data_PIN);
}
//*************************************************************************************************************
void CS1237_Data_IS_INPUT_LOW()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_Data_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_Data_PORT, &GPIO_InitStructure);
GPIO_ResetBits(CS1237_Data_PORT,CS1237_Data_PIN);
}
//*************************************************************************************************************
void CS1237_SCLK_HIHG()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_CLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_CLK_PORT, &GPIO_InitStructure);
GPIO_SetBits(CS1237_CLK_PORT,CS1237_CLK_PIN);
}
//*************************************************************************************************************
void CS1237_SCLK_LOW()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = CS1237_CLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS1237_CLK_PORT, &GPIO_InitStructure);
GPIO_ResetBits(CS1237_CLK_PORT,CS1237_CLK_PIN);
}
//*************************************************************************************************************
void C1237_OneClk()
{
CS1237_SCLK_HIHG();
delay_us(CLK_TIME);
CS1237_SCLK_LOW();
delay_us(CLK_TIME);
}
//*************************************************************************************************************
u8 CS1237_ReadReg()
{
u8 i = 0;
u8 tem = 0;
CS1237_SCLK_LOW();
delay_us(50);//Wakeup MCU
CS1237_Data_IS_INPUT(); //输入模式
while (CS1237_Data_PIN_STATE())//需要实际工程使用需要加入超时
{
// delay_us(10);//Wakeup MCU
// i++;
// if(i>20)
// {
// return 0;
// }
};
for (i = 0; i < 27; i++) // 1-26th CLK
C1237_OneClk();
CS1237_Data_IS_Out_HIGH(); //Force High cs1237 direction input
C1237_OneClk(); // 28th CLK
C1237_OneClk(); // 29th CLK
//
tem = 0xAC; // PIN out //读命令(0x56)左移1位
for (i = 0; i < 7; i++) // 30~36th CLK
{
if (tem & 0x80)
CS1237_Data_IS_Out_HIGH();
else
CS1237_Data_IS_Out_LOW();
tem <<= 1;
C1237_OneClk();
}
CS1237_Data_IS_INPUT(); // cs1237 change to output direction
C1237_OneClk(); // 37th CLK
for (i = 0; i < 8; i++) // 37~45th CLK
{
tem <<= 1;
C1237_OneClk();
if (CS1237_Data_PIN_STATE())
tem++;
}
C1237_OneClk();
; // 46th CLK
return tem;
}
// End of CS1237_ReadReg()
//*************************************************************************************************************
void C1237_WriteReg(u8 mData)
{
u8 i = 0;
u8 tem = 0;
CS1237_SCLK_LOW();
delay_us(50);//Wakeup MCU
CS1237_Data_IS_INPUT();
while (CS1237_Data_PIN_STATE())//需要实际工程使用需要加入超时
{
// delay_us(10);//Wakeup MCU
// i++;
// if(i>20)
// {
// return ;
// }
};
for (i = 0; i < 27; i++) // 1~26th CLK
C1237_OneClk();
//C1237_OneClk();//27th CLK
CS1237_Data_IS_Out_HIGH(); // force high
C1237_OneClk(); // 28th CLK
C1237_OneClk(); // 29th CLK
tem = 0xCA;//写命令(0x65)左移1位
for (i = 0; i < 7; i++) // 30~36th CLK
{
if (tem & 0x80)
CS1237_Data_IS_Out_HIGH();
else
CS1237_Data_IS_Out_LOW();
tem <<= 1;
C1237_OneClk();
}
CS1237_Data_IS_Out_HIGH(); // direction chage to intput direction
C1237_OneClk(); // 37th CLK
for (i = 0; i < 8; i++) // 38~45th CLK
{
if (mData & 0x80)
CS1237_Data_IS_Out_HIGH();
else
CS1237_Data_IS_Out_LOW();
mData <<= 1;
C1237_OneClk();
}
CS1237_Data_IS_INPUT();
C1237_OneClk(); // 46th CLK
}
// End of C1237_WriteReg(u8 mData)
//*************************************************************************************************************
extern u32 ADC_REG_REG_SET;
void C1237_Init(unsigned char refOutable, C1237_SPEED msel, C1237_PGA mselPga, C1237_CHN mselChn)
{
volatile u8 tem = 0;
tem= ((refOutable&0x1)<<6)|((msel&0x03)<<4)|((mselPga&0x03)<<2)|((mselChn&0x03)<<0);
ADC_REG_REG_SET = tem;
C1237_WriteReg(tem);
}
// End of C1237_Init(BOOL refOutable,C1237_SPEED msel,C1237_PGA mselPga,C1237_CHN mselChn)
//*************************************************************************************************************
u32 C1237_ReadResult()
{
u8 i;
u32 rdata = 0;
CS1237_SCLK_LOW();
delay_us(50);//Wakeup MCU
CS1237_Data_IS_INPUT();
while (CS1237_Data_PIN_STATE())//需要实际工程使用中断替换
; // waite ADC finish
for (i = 0; i < 24; i++)
{
rdata <<= 1;
C1237_OneClk();
if (CS1237_Data_PIN_STATE()) // read data
rdata++;
}
C1237_OneClk();
C1237_OneClk();
C1237_OneClk();
return rdata;
}
// End of C1237_ReadResult()
#ifndef __CS1237_H__
#define __CS1237_H__
//CS1237_SCLK and CS1237_DOUT were defined
#include "sys.h"
#define CLK_TIME 75 // max 50us
//************************************************************************************************************
typedef enum
{
C1237_10HZ = 0,
C1237_40HZ = 1,
C1237_640HZ = 2,
C1237_1280HZ = 3
}C1237_SPEED;
//************************************************************************************************************
typedef enum
{
C1237_PGA_1 = 0,
C1237_PGA_2 = 1,
C1237_PGA_64 = 2,
C1237_PGA_128 = 3
}C1237_PGA;
//************************************************************************************************************
typedef enum
{
C1237_CHA = 0, //adc channel
C1237_RST = 1, //cheap reserved
C1237_TEM = 2, //temperature test
C1237_INS = 3 //internal short
}C1237_CHN;
/************************************************************************************************************
Function: C1237_OneClk();
Return value: void
Discription: C1237 input a clk pusle for to read or write
Example:
C1237_OneClk();
*************************************************************************************************************/
void C1237_OneClk();
/************************************************************************************************************
Function: CS1237_ReadReg();
Return value: u8
Discription: read CS1237 register
Example:
CS1237_ReadReg();
*************************************************************************************************************/
u8 CS1237_ReadReg();
/************************************************************************************************************
Function: C1237_WriteReg(u8 mData);
Return value: void
Discription: wreite data to C1237 register
Example:
C1237_WriteReg(mData);
*************************************************************************************************************/
void C1237_WriteReg(u8 mData);
/************************************************************************************************************
Function: C1237_Init(BOOL refOutable,C1237_SPEED msel,C1237_PGA mselPga,C1237_CHN mselChn);
Return value: void
Discription: CS1237 init
Example:
C1237_Init(0,C1237_10HZ,C1237_PGA_1,C1237_CHA);//ref voltage out disable,speed 10Hz,Pga 1, work at Adc mode
*************************************************************************************************************/
void C1237_Init(unsigned char refOutable, C1237_SPEED msel, C1237_PGA mselPga, C1237_CHN mselChn);
/************************************************************************************************************
Function: C1237_ReadResult();
Return value: u32
Discription: read C1237 A/D result
Example:
C1237_ReadResult();
*************************************************************************************************************/
u32 C1237_ReadResult();
#endif