CH450驱动程序

ch450.h

#ifndef __BSP_CH450_H
#define __BSP_CH450_H
#include "stm32f4xx_gpio.h"

#define CH450_SDA_SET       GPIO_SetBits(GPIOF, GPIO_Pin_14)        // set SDA high
#define CH450_SDA_CLR       GPIO_ResetBits(GPIOF, GPIO_Pin_14)      // set SDA low

#define CH450_SCL_SET       GPIO_SetBits(GPIOF, GPIO_Pin_13)        // set SCL high
#define CH450_SCL_CLR       GPIO_ResetBits(GPIOF, GPIO_Pin_13)      // set SCL low

#define CH450_RD_SDA        GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_14)

#define CH450_DIG2      0x1200              // display DIG2
#define CH450_DIG3      0x1300              // display DIG3
#define CH450_DIG4      0x1400              // display DIG4
#define CH450_DIG5      0x1500                    // display DIG5
#define CH450_DIG6      0x1600                    // display DIG6
#define CH450_DIG7      0x1700              // display DIG7

#define CH450_OFF       0x0400          // turn off led
#define CH450_ON        0x0401          // turn on led

#define CH450_I2C_ADDR0     0x40        // address of addr0

#define CH450_I2C_MASK      0x3E        // MASK of high byte


void CH450_Init( void );
uint8_t CH450_LED1( uint16_t num );
uint8_t CH450_LED2( uint16_t num );
#endif /* __BSP_CH450_H */
 

ch450.c

#include "ch450.h"
#include "stm32f4xx_gpio.h"
#include "delay.h"

void CH450_SDA_IN()
{
    GPIO_InitTypeDef  GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
}

void CH450_SDA_OUT()
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  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(GPIOF, &GPIO_InitStructure);   
}

void CH450_SCL_OUT()
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  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(GPIOF, &GPIO_InitStructure);        
}

/***********************************************
*Function Name :CH450_I2c_Start
*Description   :I2C START
*Input         :None
*Output        :None
*Return        :None
************************************************/                                                                           
void CH450_I2c_Start( void )
{
    CH450_SDA_OUT();  /* set SDA as output */
  delay_us(2);
  CH450_SDA_SET; 
  delay_us(2);

  CH450_SCL_SET;        
  delay_us(2);

  CH450_SDA_CLR;          
  delay_us(2);

  CH450_SCL_CLR;  /* ready to transfer/receive data */        
  delay_us(2);
        
}

/***********************************************
*Function Name :CH450_I2c_Stop
*Description   :I2C STOP
*Input         :None
*Output        :None
*Return        :None
************************************************/        
void CH450_I2c_Stop( void )
{
    CH450_SDA_OUT(); /* set SDA as output */        
  delay_us(2);
  CH450_SDA_CLR;
  delay_us(2);

  CH450_SCL_SET;         
  delay_us(2);

  CH450_SDA_SET; /* transfer I2C stop signal*/        
  delay_us(2);
}

/***********************************************
*Function Name :CH450_I2c_WrByte
*Description   :I2C write one byte
*Input         :c data be written
*Output        :None
*Return        :None
************************************************/
void CH450_I2c_WrByte(uint8_t c)        
{
    uint8_t i;        
  CH450_SDA_OUT();      
  delay_us(2);
                        
  for (i = 0; i != 8; i++)
  {
        if (c & 0x80)
    {
            CH450_SDA_SET;
    }
    else
    {
            CH450_SDA_CLR;
    }
      delay_us(2);
      c <<= 1;                          
      CH450_SCL_SET;               
      
      delay_us(2);

      CH450_SCL_CLR;
      delay_us(2);
  }
     
}

void CH450_I2C_SendACK()
{
  CH450_SDA_OUT();
    CH450_SDA_SET; 
    delay_us(2);
  CH450_SCL_SET;
  delay_us(2);
    CH450_SCL_CLR;
    delay_us(2);
}

uint8_t CH450_I2C_ReceACK()
{
  CH450_SDA_IN();
  CH450_SCL_CLR;
  delay_us(2);
  CH450_SCL_SET;
  delay_us(2);
  if (CH450_RD_SDA)
  {
        delay_us(2);
        CH450_SCL_CLR;
    delay_us(2);
                        
    return 1;
  }
  else
  {
        delay_us(2);
    CH450_SCL_CLR;
    delay_us(2);
                       
    return 0;
  }
}

/***********************************************
*Function Name :CH450_Write
*Description   :CH450 write command
*Input         :command 
*Output        :None
*Return        :None
************************************************/
void CH450_Write(uint16_t command)       
{        
    CH450_I2c_Start();               
         
  CH450_I2c_WrByte(((uint8_t)(command >> 7) & CH450_I2C_MASK )| CH450_I2C_ADDR0); 
  
    while(CH450_I2C_ReceACK() == 1)
    {}        
    
  CH450_I2c_WrByte((uint8_t)command);
        
    while(CH450_I2C_ReceACK() == 1)
    {}        
         
  CH450_I2c_Stop();                 
}

const unsigned char BCD_decode_tab[0x10] = {0X3F,0X06,0X5B,0X4F,
                                                                                        0X66,0X6D,0X7D,0X07,
                                                                                        0X7F,0X6F,0X77,0X7C,
                                                                                        0X58,0X5E,0X79,0X71};


/***********************************************
*Function Name :CH450_Init
*Description   :CH450 init
*Input         :None
*Output        :None
*Return        :None
************************************************/
void CH450_Init( void )
{   
           /* Enable GPIOG's AHB interface clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

  CH450_SCL_OUT();
  delay_us(2);
    
  CH450_Write(CH450_ON);// turn on led

    CH450_I2c_Stop();    
  delay_us(2);
}

/***********************************************
*Function Name :CH450_LED1
*Description   :CH450_LED1 display
*Input         :displayed number
*Output        :None
*Return        :None
************************************************/
uint8_t CH450_LED1( uint16_t num )
{   
  uint8_t unit_place, ten_place , hundred_place ;
    
    if (num > 999)
        num = 999;

    unit_place = num / 1 % 10;
    ten_place = num / 10 % 10;    
    hundred_place = num / 100 % 10;
    
    CH450_Write(CH450_DIG4 | BCD_decode_tab[unit_place]);
    CH450_Write(CH450_DIG3 | BCD_decode_tab[ten_place]);
    CH450_Write(CH450_DIG2 | BCD_decode_tab[hundred_place]);

    return 0;
}

/***********************************************
*Function Name :CH450_LED2
*Description   :CH450_LED2 display
*Input         :displayed number
*Output        :None
*Return        :None
************************************************/
uint8_t CH450_LED2( uint16_t num )
{   
  uint8_t unit_place, ten_place , hundred_place ;
    
    if (num > 999)
        num = 999;

    unit_place = num / 1 % 10;
    ten_place = num / 10 % 10;    
    hundred_place = num / 100 % 10;
    
    CH450_Write(CH450_DIG7 | BCD_decode_tab[unit_place]);
    CH450_Write(CH450_DIG6 | BCD_decode_tab[ten_place]);
    CH450_Write(CH450_DIG5 | BCD_decode_tab[hundred_place]);

    return 0;
}

delay.h

#ifndef _DELAY_H_
#define _DELAY_H_

void delay_us(uint16_t us);
void delay_ms(uint16_t ms);

#endif

delay.c

#include "stm32f4xx.h"


__asm void SysCtlDelay(uint16_t ulCount)
{
    subs    r0, #1;
    bne     SysCtlDelay;
    bx      lr;
}

void delay_us(uint16_t us)
{
    SysCtlDelay(56 * us);
}

void delay_ms(uint16_t ms)
{
    int i;
    
    for(i = 0; i < ms; i++)
    {
        delay_us(1000);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值