1.开发环境
(1)IAR8.4
(2)MPU6050 1.8寸显示屏
2.软件
(1)MPU6050陀螺仪驱动
i2C配置:
void i2c0_gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_I2C0);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
}
void i2c_config(void)
{
i2c_clock_config(I2C0, 200000, I2C_DTCY_2);
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, SlaveAddress);
i2c_enable(I2C0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
uint8_t Read_Byte(uint16_t SlaveAddr,uint16_t ReadAddr, uint8_t *DataByte)
{
uint8_t err = 1;
i2c_start_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C0, SlaveAddr<<1, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, ReadAddr);
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_start_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C0, SlaveAddr<<1, I2C_RECEIVER);
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
i2c_stop_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
*DataByte = i2c_data_receive(I2C0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
err = 0;
return err;
}
uint8_t Write_Byte(uint16_t SlaveAddr,uint16_t WriteAddr,uint16_t WriteByte)
{
uint8_t err = 0;
i2c_start_on_bus(I2C0);
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C0, SlaveAddr<<1, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, WriteAddr);
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_data_transmit(I2C0, WriteByte);
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
i2c_stop_on_bus(I2C0);
return err;
}
陀螺仪初始化:
void mpu6050_init(){
Write_Byte( SlaveAddress, PWR_MGMT_1, 0x00 );
delay_1ms(10);
Write_Byte( SlaveAddress, SMPLRT_DIV, 0x07 );
delay_1ms(10);
Write_Byte( SlaveAddress, CONFIG, 0x06 );
delay_1ms(10);
Write_Byte( SlaveAddress, GYRO_CONFIG, 0x18);
delay_1ms(10);
Write_Byte( SlaveAddress, ACCEL_CONFIG, 0x01);
delay_1ms(10);
}
void GetData(uint16_t SlaveAddr,uint16_t REG_Address,short data[3])
{
uint8_t H=0,L=0;
for(int i=0;i<3;i++){
Read_Byte(SlaveAddress, REG_Address+(i*2), &H);
Read_Byte( SlaveAddress, REG_Address+(i*2+1),&L);
data[i]=(( short)(H<<8))|L;
}
(2)LCD驱动
驱动:
void LCD_GPIO_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
gpio_bit_set(GPIOA,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
}
void LCD_Writ_Bus(uint8_t dat)
{
uint8_t i;
LCD_CS_Clr();
for(i=0;i<8;i++)
{
LCD_SCLK_Clr();
if(dat&0x80)
{
LCD_MOSI_Set();
}
else
{
LCD_MOSI_Clr();
}
LCD_SCLK_Set();
dat<<=1;
}
LCD_CS_Set();
}
void LCD_WR_DATA8(uint8_t dat)
{
LCD_Writ_Bus(dat);
}
void LCD_WR_DATA(uint16_t dat)
{
LCD_Writ_Bus(dat>>8);
LCD_Writ_Bus(dat);
}
void LCD_WR_REG(uint8_t dat)
{
LCD_DC_Clr();//д????
LCD_Writ_Bus(dat);
LCD_DC_Set();//д????
}
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
if(USE_HORIZONTAL==0)
{
LCD_WR_REG(0x2a);//?е??????
LCD_WR_DATA(x1+2);
LCD_WR_DATA(x2+2);
LCD_WR_REG(0x2b);//?е??????
LCD_WR_DATA(y1+1);
LCD_WR_DATA(y2+1);
LCD_WR_REG(0x2c);//??????д
}
else if(USE_HORIZONTAL==1)
{
LCD_WR_REG(0x2a);//?е??????
LCD_WR_DATA(x1+2);
LCD_WR_DATA(x2+2);
LCD_WR_REG(0x2b);//?е??????
LCD_WR_DATA(y1+1);
LCD_WR_DATA(y2+1);
LCD_WR_REG(0x2c);//??????д
}
else if(USE_HORIZONTAL==2)
{
LCD_WR_REG(0x2a);//?е??????
LCD_WR_DATA(x1+1);
LCD_WR_DATA(x2+1);
LCD_WR_REG(0x2b);//?е??????
LCD_WR_DATA(y1+2);
LCD_WR_DATA(y2+2);
LCD_WR_REG(0x2c);//??????д
}
else
{
LCD_WR_REG(0x2a);//?е??????
LCD_WR_DATA(x1+1);
LCD_WR_DATA(x2+1);
LCD_WR_REG(0x2b);//?е??????
LCD_WR_DATA(y1+2);
LCD_WR_DATA(y2+2);
LCD_WR_REG(0x2c);//??????д
}
}
void LCD_Init(void)
{
LCD_GPIO_Init();
LCD_RES_Clr();
delay_1ms(100);
LCD_RES_Set();
delay_1ms(100);
LCD_BLK_Set();
delay_1ms(100);
delay_1ms(120);
LCD_WR_REG(0xB1);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3C);
LCD_WR_DATA8(0x3C);
LCD_WR_REG(0xB2);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3C);
LCD_WR_DATA8(0x3C);
LCD_WR_REG(0xB3);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3C);
LCD_WR_DATA8(0x3C);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3C);
LCD_WR_DATA8(0x3C);
LCD_WR_REG(0xB4); //Dot inversion
LCD_WR_DATA8(0x03);
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0x28);
LCD_WR_DATA8(0x08);
LCD_WR_DATA8(0x04);
LCD_WR_REG(0xC1);
LCD_WR_DATA8(0XC0);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x00);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0x2A);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0xEE);
LCD_WR_REG(0xC5); //VCOM
LCD_WR_DATA8(0x1A);
LCD_WR_REG(0x36); //MX, MY, RGB mode
if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
else LCD_WR_DATA8(0xA0);
LCD_WR_REG(0xE0);
LCD_WR_DATA8(0x04);
LCD_WR_DATA8(0x22);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x2E);
LCD_WR_DATA8(0x30);
LCD_WR_DATA8(0x25);
LCD_WR_DATA8(0x2A);
LCD_WR_DATA8(0x28);
LCD_WR_DATA8(0x26);
LCD_WR_DATA8(0x2E);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x13);
LCD_WR_REG(0xE1);
LCD_WR_DATA8(0x04);
LCD_WR_DATA8(0x16);
LCD_WR_DATA8(0x06);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x2D);
LCD_WR_DATA8(0x26);
LCD_WR_DATA8(0x23);
LCD_WR_DATA8(0x27);
LCD_WR_DATA8(0x27);
LCD_WR_DATA8(0x25);
LCD_WR_DATA8(0x2D);
LCD_WR_DATA8(0x3B);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x01);
LCD_WR_DATA8(0x04);
LCD_WR_DATA8(0x13);
//------------------------------------End ST7735S Gamma Sequence-----------------------------//
LCD_WR_REG(0x3A); //65k mode
LCD_WR_DATA8(0x05);
LCD_WR_REG(0x29); //Display on
}
显示函数:
void LCD_Fill(uint16_t xsta,uint16_t ysta,uint16_t xend,uint16_t yend,uint16_t color)
{
uint16_t i,j;
LCD_Address_Set(xsta,ysta,xend-1,yend-1);//?????????Χ
for(i=ysta;i
}
void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
{
LCD_Address_Set(x,y,x,y);//???ù??λ??
LCD_WR_DATA(color);
}
void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color)
{
uint16_t t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //????????????
delta_y=y2-y1;
uRow=x1;//???????????
uCol=y1;
if(delta_x>0)incx=1; //???????????
else if (delta_x==0)incx=0;//?????
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if (delta_y==0)incy=0;//????
else {incy=-1;delta_y=-delta_y;}
if(delta_x>delta_y)distance=delta_x; //????????????????
else distance=delta_y;
for(t=0;tdistance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color)
{
LCD_DrawLine(x1,y1,x2,y1,color);
LCD_DrawLine(x1,y1,x1,y2,color);
LCD_DrawLine(x1,y2,x2,y2,color);
LCD_DrawLine(x2,y1,x2,y2,color);
}
void Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r,uint16_t color)
上一篇:从零使用env工具生成gd32f450工程
下一篇:最后一页
推荐阅读最新更新时间:2024-11-11 13:33
设计资源 培训 开发板 精华推荐
- EVAL-ADM1031,基于 ACPI 温度监视器和 ADM1031 PWM 风扇控制器的评估板
- LTC3622IDE-2 5V/3.3V Vout 突发模式操作应用同步降压稳压器的典型应用电路
- NCP1522BUGEVB:3.0 MHz、600 mA 降压 DC-DC 转换器评估板
- 结合温度补偿的超声波测距装置设计
- LTC6262IMS8 235 uA 电源电流运算放大器的典型应用
- 使用 NXP Semiconductors 的 XPC860DE 的参考设计
- 使用 Analog Devices 的 LTC3607EMSE 的参考设计
- 安全监控面板
- 【立创开发板】基于梁山派的小掌机
- LT8494HFE 450kHz、宽输入范围 12V 输出 SEPIC 转换器的典型应用电路