GD32F310定时器采样MP6050并在彩色LCD上显示波形

发布者:快乐舞动最新更新时间:2024-11-11 来源: elecfans关键字:显示波形 手机看文章 扫描二维码
随时随地手机看文章

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)

[1] [2]
关键字:显示波形 引用地址:GD32F310定时器采样MP6050并在彩色LCD上显示波形

上一篇:从零使用env工具生成gd32f450工程
下一篇:最后一页

推荐阅读最新更新时间:2024-11-11 13:33

为什么示波器屏幕上显示的是很多波形会叠在一起?
这是示波器波形刷新率与屏幕的显示波形关系。 示波器屏幕的刷新速度为 50-60 Hz。示波器的波形更新率很快,所以在屏幕上显示的是很多波形会叠在一起的波形。例如,是德科技 3000X 示波器的波形更新率高达每秒一百万次。
[测试测量]
小广播
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved