/***************COPYRIGHT2011***************************
*File Name :ch450.c
*Author :gaochs
*Description :stm32控制ch450
stm32IO口分配:
SDA = PB11 SCL = PB10 INT = PE0
微调旋钮(INT2_L = PB8 INT3_R = PB9)
蜂鸣器 = PE1
*Vision :V1.0.0
*Last Modify:
*--------------------------------------------------------
*Modification History
*********************************************************/
#include "stm32f4xx.h"
#include "CH450.h"
/*********************************************************
Variable Name :CH450_xxx
Dscription :define CH450 IO
**********************************************************/
#define CH450_SDA_SET GPIO_SetBits(GPIOE, GPIO_Pin_5) //SDA置高
#define CH450_SDA_CLR GPIO_ResetBits(GPIOE, GPIO_Pin_5) //SDA置低
#define CH450_SCL_SET GPIO_SetBits(GPIOE, GPIO_Pin_6) //SCL置高
#define CH450_SCL_CLR GPIO_ResetBits(GPIOE, GPIO_Pin_6) //SCL置低
#define CH450_RD_SDA GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5)
#define DELAY mDelayuS(1000)
#define CH450_I2C_ADDR0 0x40 // CH450的ADDR=0时的地址
#define CH450_I2C_MASK 0x3E // CH450的2线接口高字节命令掩码
#define CH450_GET_KEY 0x0700 // 获取按键,返回按键代码
// *******************
// 类型定义
// *******************
// **************************************
// CH450的全局变量End
// **************************************
void CH450_SDA_IN()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void CH450_SDA_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void CH450_SCL_OUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
/***********************************************
*Function Name :mDelayuS
*Description :0.04us
*Input :n 延时时间
*Output :None
*Return :None
************************************************/
void mDelayuS(u16 n)
{
while( --n )
{
;
}
}
/***********************************************
*Function Name :mDelaymS
*Description :43us
*Input :delay 延时时间
*Output :None
*Return :None
************************************************/
void mDelaymS(u16 delay)
{
u16 ms;
u16 sm;
u16 c;
for(ms = delay; ms != 0; ms--)
{
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 240; sm != 0; sm--) c += 3;
}
for (ms = delay; ms != 0; ms--)
{
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 200; sm != 0; sm--) c += 3;
for (sm = 240; sm != 0; sm--) c += 3;
}
}
/***********************************************
*Function Name :CH450_I2c_Start
*Description :I2C START
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Start( void )
{
CH450_SDA_OUT(); /* 设置SDA为输出方向 */
DELAY;
CH450_SDA_SET; /*发送起始条件的数据信号*/
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SDA_CLR; /*发送起始信号*/
DELAY;
CH450_SCL_CLR; /*钳住I2C总线,准备发送或接收数据 */
DELAY;
}
/***********************************************
*Function Name :CH450_I2c_Stop
*Description :I2C STOP
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Stop( void )
{
CH450_SDA_OUT(); /* 设置SDA为输出方向 */
DELAY;
CH450_SDA_CLR;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SDA_SET; /*发送I2C总线结束信号*/
DELAY;
}
/***********************************************
*Function Name :CH450_I2c_WrByte
*Description :I2C 写一个字节
*Input :c 待写字节数据
*Output :None
*Return :None
************************************************/
void CH450_I2c_WrByte(u8 c) //写一个字节数据
{
u8 i;
CH450_SDA_OUT(); /* 设置SDA为输出方向 */
DELAY;
for (i = 0; i != 8; i++)
{
if (c & 0x80)
{
CH450_SDA_SET;
}
else
{
CH450_SDA_CLR;
}
DELAY;
CH450_SCL_SET;
c <<= 1;
DELAY;
CH450_SCL_CLR;
DELAY;
}
CH450_SDA_IN(); /* 设置SDA为输入方向 */
DELAY;
CH450_SCL_SET; //接收应答
DELAY;
CH450_SCL_CLR;
DELAY;
}
void CH450_I2C_SendACK()
{
CH450_SDA_OUT();
CH450_SCL_CLR;
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SCL_CLR;
DELAY;
CH450_SDA_CLR;
}
u8 CH450_I2C_ReceACK()
{
CH450_SDA_IN();
CH450_SCL_CLR;
DELAY;
CH450_SCL_SET;
DELAY;
if (CH450_RD_SDA)
{
CH450_SCL_CLR;
DELAY;
//CH450_PORT &= ~SDA;
return 1;
}
else
{
CH450_SCL_CLR;
DELAY;
//CH450_PORT &= ~SDA;
return 0;
}
}
/***********************************************
*Function Name :CH450_I2c_RdByte
*Description :I2C 读一个字节
*Input :None
*Output :None
*Return :dat
************************************************/
u8 CH450_I2c_RdByte(void) //读一个字节数据
{
u8 dat,i;
u16 j;
CH450_SDA_OUT(); //CPU释放SDA引脚
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SDA_IN(); /* 设置SDA为输入方向 */
DELAY;
dat = 0;
for (i = 0; i != 8; i++) // 输入8位数据
{
CH450_SCL_SET;
DELAY;
dat <<= 1;
j = CH450_RD_SDA;
if (j)
{
dat++;
} // 输入1位
DELAY;
CH450_SCL_CLR;
DELAY;
}
CH450_SDA_OUT();
DELAY;
CH450_SDA_SET;
DELAY;
CH450_SCL_SET;
DELAY;
CH450_SCL_CLR;
DELAY;
return(dat);
}
/***********************************************
*Function Name :CH450_Write
*Description :CH450写命令
*Input :command 写命令
*Output :None
*Return :None
************************************************/
void CH450_Write(u16 command) //写命令
{
CH450_I2c_Start(); /*启动总线*/
CH450_I2c_WrByte((u8)(command >> 7) & CH450_I2C_MASK | CH450_I2C_ADDR0); // CH450的ADDR=0时
CH450_I2c_WrByte((u8)command); /*发送数据*/
CH450_I2c_Stop(); /*结束总线*/
}
/***********************************************
*Function Name :CH450_Read
*Description :CH450读命令
*Input :None
*Output :None
*Return :val
************************************************/
u8 CH450_Read( void)
{
u8 val;
CH450_I2c_Start();/*启动总线*/
DELAY;
// CH450_I2c_WrByte((u8)(CH450_GET_KEY>>7)&CH450_I2C_MASK|CH450_I2C_ADDR0|0x01); // 若有两个CH450并连,当ADDR=0时,需修改为CH450_I2C_ADDR0
CH450_I2c_WrByte(0x4F);//读取按键代码输出字节1
DELAY;
val = CH450_I2c_RdByte(); /*读取数据*/
CH450_I2c_Stop(); /*结束总线*/
DELAY;
return val;
}
/***********************************************
*Function Name :CH450_Init
*Description :CH450的初始化函数
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_Init( void )
{
/* Enable GPIOG's AHB interface clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
CH450_SCL_OUT();
DELAY;
CH450_Write(0x0403);//开显示键盘
DELAY;
}
#ifndef _CH450_H_
#define _CH450_H_
#include "stm32f4xx.h"
/***********************************************
*Function Name :mDelayuS
*Description :
*Input :n 延时时间
*Output :None
*Return :None
************************************************/
void mDelayuS(u16 n);
/***********************************************
*Function Name :mDelaymS
*Description :
*Input :delay 延时时间
*Output :None
*Return :None
************************************************/
void mDelaymS(u16 delay);
/***********************************************
*Function Name :CH450_I2c_Start
*Description :I2C START
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Start( void );
/***********************************************
*Function Name :CH450_I2c_Stop
*Description :I2C STOP
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_I2c_Stop( void );
/***********************************************
*Function Name :CH450_I2c_WrByte
*Description :I2C 写一个字节
*Input :c 待写字节数据
*Output :None
*Return :None
************************************************/
void CH450_I2c_WrByte(u8 c);
/***********************************************
*Function Name :CH450_I2c_RdByte
*Description :I2C 读一个字节
*Input :None
*Output :None
*Return :dat
************************************************/
u8 CH450_I2c_RdByte( void );
/***********************************************
*Function Name :CH450_Write
*Description :CH450写命令
*Input :command 写命令
*Output :None
*Return :None
************************************************/
void CH450_Write(u16 command);
/***********************************************
*Function Name :CH450_Read
*Description :CH450读命令
*Input :None
*Output :None
*Return :val
************************************************/
u8 CH450_Read( void);
/***********************************************
*Function Name :CH450_Init
*Description :CH450的初始化函数
*Input :None
*Output :None
*Return :None
************************************************/
void CH450_Init( void );
#endif
|