(4)mbed入门——iic控制1602
时间:10-02
整理:3721RD
点击:
首先声明,所有代码都是自己手打的,原创首发!
第一次写C++程序(花了一晚上,照葫芦画瓢写得)先上基本效果
开始用c写的,没用过c++,mbed编译总报错,各种错,没办法了,完全照着改的,没想到成功了
这里是主程序:
- #include "mbed.h"
- #include "1602_iic_sw.h"
- #define GREEN_LED PB_0
- #define BLUE_LED PB_7
- #define RED_LED PB_14
- //DigitalOut myled1(GREEN_LED);
- //DigitalOut myled2(BLUE_LED);
- //DigitalOut myled3(RED_LED);
- PwmOut PWM1(GREEN_LED);
- PwmOut PWM2(BLUE_LED);
- PwmOut PWM3(RED_LED);
- // I2C_SCL = PB_8,
- // I2C_SDA = PB_9,
- I2C i2c_port(I2C_SDA, I2C_SCL);
- float gValue = 0;
- char data = 0;
- Serial pc(PA_2,PA_3);
- int main() {
- PWM1.period(0.01);
- PWM2.period(0.01);
- PWM3.period(0.01);
- while(1) {
- PWM1 = gValue;
- PWM2 = 1 - gValue;
- PWM3 = gValue;
- gValue = gValue + 0.001;
- wait(0.001); // 1 sec
- if(gValue >= 1)
- {
- static char ack_value = 0;
- gValue = 0;
-
- i2c_port.start();
- // ack_value = i2c_port.write(address, data, 1, 0);
- ack_value = i2c_port.write(ADDRWRITE);
- i2c_port.write(data);
- i2c_port.stop();
- printf("ACK = %d\n", ack_value);
- }
- }
- }
初始化过程有点粗暴!哈哈,直接抓协议搞的,像我这么干的恐怕不多
- #include "mbed.h"
- #include "1602_iic_sw.h"
- //set default status of backlight to ON
- static u8 backLight = BACKLIGHT_ON;
- const u8 Ainit[42] = {
- 0x30, 0x34, 0x30,
- 0x30, 0x34, 0x30,
- 0x30, 0x34, 0x30,
- 0x20, 0x24, 0x20,
- 0x20, 0x24, 0x20,
- 0x80, 0x84, 0x80,
- 0x00, 0x04, 0x00,
- 0xc0, 0xc4, 0xc0,
- 0x00, 0x04, 0x00,
- 0x10, 0x14, 0x10,
- 0x00, 0x04, 0x00,
- 0x60, 0x64, 0x60,
- 0x00, 0x04, 0x00,
- 0x20, 0x24, 0x20,
- };
- /***
- initial 1602 pins and mode
- ***/
- // Servo::Servo(PinName Pin) : ServoPin(Pin)
- IIC_1602::IIC_1602(PinName sda, PinName scl):i2c_port(sda, scl)
- {
- u8 i = 0;
- i2cAction(0);
- wait_ms(500);
- for(i = 0; i < 42; ++i)
- {
- i2cAction(Ainit[i]);
-
- if((i == 2) || (i == 5))wait_ms(5);
- else if((i == 29) || (i == 41))wait_ms(2);
- else if(i == 8)wait_us(230);
- else wait_us(20);
- }
- }
- /***
- Output charactors on 1602
- ***/
- void IIC_1602::lcdPrint(const char* charactor)
- {
- u8 c = 0;
- for(c = 0; c < (u8)strlen(charactor); ++c)
- writeOneChar(DATA_1602 ,*(charactor + c));
- }
- void IIC_1602::lcdPrint_int(const u16 val)
- {
- u8 status = 0;
- u8 num[5] = {0};
- num[4] = val/10000;
- num[3] = val/1000%10;
- num[2] = val/100%10;
- num[1] = val/10%100%10;
- num[0] = val%10;
- if(num[4] || status)writeOneChar(DATA_1602 , num[4] + 0x30),status = 1;
- if(num[3] || status)writeOneChar(DATA_1602 , num[3] + 0x30),status = 1;
- if(num[2] || status)writeOneChar(DATA_1602 , num[2] + 0x30),status = 1;
- if(num[1] || status)writeOneChar(DATA_1602 , num[1] + 0x30),status = 1;
- if(num[0] || status)writeOneChar(DATA_1602 , num[0] + 0x30),status = 1;
- writeOneChar(DATA_1602 , num[3] + 30);
- }
- void IIC_1602::disOneChar(unsigned char X, unsigned char Y, char word)
- {
- Y &= 0x1;
- X &= 0xF; //限制 X 不能大于 15,Y 不能大于 1
- if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
- X |= 0x80; //算出指令码
- writeOneChar(CMD_1602, X); //这里不检测忙信号,发送地址码
- writeOneChar(DATA_1602, word);
- }
- void IIC_1602::writeOneChar(const u8 cmdOrData, const u8 Data)
- {
- u8 LCM_Data = 0;
- LCM_Data = ((Data & 0xF0) | (cmdOrData) | (backLight));
- i2cAction(LCM_Data);
- i2cAction(LCM_Data | 0x04);
- wait_us(20);
- LCM_Data = (((Data & 0x0F)<<4) | (cmdOrData) | (backLight));
- i2cAction(LCM_Data);
- i2cAction(LCM_Data | 0x04);
- wait_us(20);
- i2cAction(LCM_Data);
- }
- /***
- set position of cursor
- ***/
- void IIC_1602::setCursor(u8 colmn, u8 line)
- {
- line &= 0x1;
- colmn &= 0xF; //限制 X 不能大于 15,line 不能大于 1
- if (line) colmn |= 0x40; //当要显示第二行时地址码+0x40;
- colmn |= 0x80; //算出指令码
- writeOneChar(CMD_1602, colmn); //这里不检测忙信号,发送地址码
- }
- /***
- send one frame by iic
- ***/
- void IIC_1602::i2cAction(u8 status)
- {
- i2c_port.start();
- i2c_port.write(ADDRWRITE);
- i2c_port.write(status);
- i2c_port.stop();
- }
- #ifndef __1602_IIC_SW_H__
- #define __1602_IIC_SW_H__
- #include "mbed.h"
- #include "string.h"
- #ifndef u8
- #define u8 unsigned char
- #endif
- #ifndef u16
- #define u16 unsigned int
- #endif
- #define ADDR 0x27
- //High bit to R,Loe bit to W
- #define ADDRREAD ((ADDR<<1) | 0x01)
- #define ADDRWRITE (ADDR<<1)
- #define BACKLIGHT_ON 0x08
- #define BACKLIGHT_OFF 0x00
- #define LCD_CLC 0x00
- #define CMD_1602 0
- #define DATA_1602 1
- class IIC_1602 {
- public:
- IIC_1602(PinName sda, PinName scl);
- void lcdPrint(const char* charactor);
- void lcdPrint_int(const u16 val);
- void disOneChar(unsigned char X, unsigned char Y, char word);
- void writeOneChar(const u8 cmdOrData, const u8 Data);
- void setCursor(u8 colmn, u8 line);
- void i2cAction(u8 status);
- private :
- I2C i2c_port;
- };
- #endif