433 lines
10 KiB
C
433 lines
10 KiB
C
![]() |
#include <stdio.h>
|
|||
|
#include "ohos_init.h"
|
|||
|
#include "iot_gpio.h"
|
|||
|
#include "iot_gpio_ex.h"
|
|||
|
|
|||
|
#include "iot_i2c.h"
|
|||
|
|
|||
|
#include "cmsis_os2.h"
|
|||
|
|
|||
|
|
|||
|
// #include "ohos_init.h"
|
|||
|
// #include <stdio.h>
|
|||
|
#include <string.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <unistd.h>
|
|||
|
// #include <cmsis_os2.h>
|
|||
|
#include <stdbool.h>
|
|||
|
|
|||
|
|
|||
|
#define IIC_SDA 0
|
|||
|
#define IIC_SCL 1
|
|||
|
#define ADDR 0x27 // 0100111
|
|||
|
#define IIC_IDX 1
|
|||
|
|
|||
|
/*
|
|||
|
Command Register
|
|||
|
0 Input port 0
|
|||
|
1 Input port 1
|
|||
|
2 Output port 0
|
|||
|
3 Output port 1
|
|||
|
4 Polarity Inversion port 0
|
|||
|
5 Polarity Inversion port 1
|
|||
|
6 Configuration port 0
|
|||
|
7 Configuration port 1
|
|||
|
*/
|
|||
|
#define CMD_CFG0 6
|
|||
|
#define CMD_CFG1 7
|
|||
|
#define CMD_OUT0 2
|
|||
|
#define CMD_OUT1 3
|
|||
|
|
|||
|
|
|||
|
uint32_t exec1, exec2, exec3;
|
|||
|
|
|||
|
/***** 定时器1 回调函数 *****/
|
|||
|
void Timer1_Callback(void *arg)
|
|||
|
{
|
|||
|
(void)arg;
|
|||
|
//printf("This is Timer1_Callback!\r\n");
|
|||
|
}
|
|||
|
|
|||
|
/***** 定时器2 回调函数 *****/
|
|||
|
void Timer2_Callback(void *arg)
|
|||
|
{
|
|||
|
(void)arg;
|
|||
|
//printf("This is Timer2_Callback!\r\n");
|
|||
|
}
|
|||
|
|
|||
|
/***** 定时器3 回调函数 *****/
|
|||
|
void Timer3_Callback(void *arg)
|
|||
|
{
|
|||
|
(void)arg;
|
|||
|
//printf("This is Timer3_Callback!\r\n");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
int write_iic(uint8_t* data){
|
|||
|
int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
|
|||
|
//printf("***@@@###$$$ ret = %d\n",ret);
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
//start
|
|||
|
uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //配置为输出
|
|||
|
uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //配置为输出
|
|||
|
uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // 输出
|
|||
|
uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // 输出
|
|||
|
|
|||
|
char alpha[8][9] = {
|
|||
|
"11111111",
|
|||
|
"11100000",
|
|||
|
"11100000",
|
|||
|
"11111111",
|
|||
|
"11111111",
|
|||
|
"11100000",
|
|||
|
"11100000",
|
|||
|
"11111111"
|
|||
|
};
|
|||
|
|
|||
|
// char alpha[8][9] = {
|
|||
|
// "00011111",
|
|||
|
// "11100000",
|
|||
|
// "11100000",
|
|||
|
// "00011111",
|
|||
|
// "00011111",
|
|||
|
// "11100000",
|
|||
|
// "11100000",
|
|||
|
// "00011111"
|
|||
|
// };
|
|||
|
|
|||
|
|
|||
|
void write_data(char byte1,char byte2){
|
|||
|
//printf("write %02x ,%02x\n",byte1,byte2);
|
|||
|
|
|||
|
uint8_t data[3] = {CMD_OUT0,0x00,0x00};
|
|||
|
data[1] = byte1;
|
|||
|
data[2] = byte2;
|
|||
|
write_iic(data);
|
|||
|
|
|||
|
data[0] = CMD_OUT1;
|
|||
|
write_iic(data);
|
|||
|
|
|||
|
//printf("write %02x ,%02x\n",byte1,byte2);
|
|||
|
// printf("data[3] = %c\n",data[3]);
|
|||
|
// printf("data[2] = %c\n",data[2]);
|
|||
|
// printf("data[1] = %c\n",data[1]);
|
|||
|
// printf("data[0] = %c\n",data[0]);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
void iic(void* args ){
|
|||
|
int i,j;
|
|||
|
|
|||
|
|
|||
|
osTimerId_t id1, id2, id3;
|
|||
|
uint32_t timerDelay;
|
|||
|
osStatus_t status;
|
|||
|
|
|||
|
// exec1 = 1U;
|
|||
|
// id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
|
|||
|
// if (id1 != NULL)
|
|||
|
// {
|
|||
|
|
|||
|
// printf(" id1 != NULL\r\n");
|
|||
|
// // Hi3861 1U=10ms,100U=1S
|
|||
|
// timerDelay = 100U;
|
|||
|
// printf(" start status1\r\n ");
|
|||
|
// status = osTimerStart(id1, timerDelay);
|
|||
|
// printf(" ******end status1\r\n ");
|
|||
|
// if (status != osOK)
|
|||
|
// {
|
|||
|
// // Timer could not be started
|
|||
|
// printf(" Timer could not be started\r\n ");
|
|||
|
// }else{
|
|||
|
// printf(" Timer could be started\r\n "); //have
|
|||
|
// }
|
|||
|
// printf(" end status1\r\n "); //have
|
|||
|
// }else{
|
|||
|
// printf(" id1 = NULL\r\n ");
|
|||
|
// }
|
|||
|
|
|||
|
// exec2 = 1U;
|
|||
|
// id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
|
|||
|
// if (id2 != NULL)
|
|||
|
// {
|
|||
|
|
|||
|
// printf(" id2 != NULL\r\n ");
|
|||
|
// // Hi3861 1U=10ms,300U=3S
|
|||
|
// timerDelay = 300U;
|
|||
|
|
|||
|
// printf(" start status2\r\n ");
|
|||
|
// status = osTimerStart(id2, timerDelay);
|
|||
|
// printf(" *******end status2\r\n ");
|
|||
|
// if (status != osOK)
|
|||
|
// {
|
|||
|
// // Timer could not be started
|
|||
|
// printf(" Timer could not be started \r\n");
|
|||
|
// }else{
|
|||
|
// printf(" Timer could be started\r\n ");
|
|||
|
// }
|
|||
|
// printf(" end status2\r\n ");
|
|||
|
// }else{
|
|||
|
// printf(" id2 = NULL\r\n ");
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
printf("iic thread running...");
|
|||
|
IoTGpioInit(IIC_SDA);
|
|||
|
IoTGpioInit(IIC_SCL);
|
|||
|
IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
|
|||
|
IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
|
|||
|
IoTI2cInit(IIC_IDX, 400000);
|
|||
|
|
|||
|
write_iic(CFG0);
|
|||
|
write_iic(CFG1);
|
|||
|
|
|||
|
//usleep(20);
|
|||
|
//usleep(200*1000);
|
|||
|
|
|||
|
exec1 = 1U;
|
|||
|
id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
|
|||
|
if (id1 != NULL)
|
|||
|
{
|
|||
|
|
|||
|
printf(" id1 != NULL\r\n");
|
|||
|
// Hi3861 1U=10ms,100U=1S
|
|||
|
timerDelay = 100U;
|
|||
|
printf(" start status1\r\n ");
|
|||
|
status = osTimerStart(id1, timerDelay);
|
|||
|
printf(" ******end status1\r\n ");
|
|||
|
if (status != osOK)
|
|||
|
{
|
|||
|
// Timer could not be started
|
|||
|
printf(" Timer could not be started\r\n ");
|
|||
|
}else{
|
|||
|
printf(" Timer could be started\r\n "); //have
|
|||
|
}
|
|||
|
printf(" end status1\r\n "); //have
|
|||
|
}else{
|
|||
|
printf(" id1 = NULL\r\n ");
|
|||
|
}
|
|||
|
|
|||
|
write_iic(OUT0);
|
|||
|
write_iic(OUT1);
|
|||
|
//usleep(1000*1000);
|
|||
|
//usleep(100);
|
|||
|
|
|||
|
|
|||
|
exec2 = 1U;
|
|||
|
id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
|
|||
|
if (id2 != NULL)
|
|||
|
{
|
|||
|
|
|||
|
printf(" id2 != NULL\r\n ");
|
|||
|
// Hi3861 1U=10ms,300U=3S
|
|||
|
timerDelay = 300U;
|
|||
|
|
|||
|
printf(" start status2\r\n ");
|
|||
|
status = osTimerStart(id2, timerDelay);
|
|||
|
printf(" *******end status2\r\n ");
|
|||
|
if (status != osOK)
|
|||
|
{
|
|||
|
// Timer could not be started
|
|||
|
printf(" Timer could not be started \r\n");
|
|||
|
}else{
|
|||
|
printf(" Timer could be started\r\n ");
|
|||
|
}
|
|||
|
printf(" end status2\r\n ");
|
|||
|
}else{
|
|||
|
printf(" id2 = NULL\r\n ");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
while(1){
|
|||
|
|
|||
|
for(int i=0;i<8;i++){
|
|||
|
unsigned char hex = 0;
|
|||
|
for(int j=0;j<8;j++){
|
|||
|
hex = hex <<1;
|
|||
|
if(alpha[i][j] == '1'){
|
|||
|
hex = hex | 0x1;
|
|||
|
}
|
|||
|
}
|
|||
|
OUT1[2] = hex;
|
|||
|
OUT0[1] = ~(1 << i);
|
|||
|
write_data(~(1 << i),hex);
|
|||
|
|
|||
|
|
|||
|
//usleep(100); //延时,肉眼无法观察的速度
|
|||
|
//alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
|
|||
|
//alpha[i][j] == '0';
|
|||
|
// char alpha[8][9] = {
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000",
|
|||
|
// "00000000"
|
|||
|
// };
|
|||
|
//hex = 0; //消隐,防止给下次扫描带来影响
|
|||
|
//usleep(100);
|
|||
|
|
|||
|
// printf("OUT1[2] = %c\n",OUT1[2]);
|
|||
|
// printf("OUT1[1] = %c\n",OUT1[1]);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//usleep(10000); //延时,肉眼无法观察的速度
|
|||
|
|
|||
|
exec3 = 1U;
|
|||
|
id3 = osTimerNew(Timer3_Callback, osTimerPeriodic, &exec3, NULL);
|
|||
|
if (id3 != NULL)
|
|||
|
{
|
|||
|
|
|||
|
printf(" id3 != NULL\r\n ");
|
|||
|
// Hi3861 1U=10ms,300U=3S
|
|||
|
timerDelay = 300U;
|
|||
|
|
|||
|
printf(" start status3\r\n ");
|
|||
|
status = osTimerStart(id3, timerDelay);
|
|||
|
printf(" *******end status3\r\n ");
|
|||
|
if (status != osOK)
|
|||
|
{
|
|||
|
// Timer could not be started
|
|||
|
printf(" Timer could not be started \r\n");
|
|||
|
}else{
|
|||
|
printf(" Timer could be started\r\n ");
|
|||
|
}
|
|||
|
printf(" end status3\r\n ");
|
|||
|
}
|
|||
|
// else
|
|||
|
// {
|
|||
|
// printf(" id3 = NULL\r\n ");
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
|
|||
|
//osDelay(100);
|
|||
|
alpha[i][j] = '0';
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void iic_entry(){
|
|||
|
printf("iic_entry called \n");
|
|||
|
|
|||
|
osThreadAttr_t attr;
|
|||
|
|
|||
|
attr.name = "thread_iic";
|
|||
|
attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
|
|||
|
attr.cb_mem = NULL; //控制快的指针
|
|||
|
attr.cb_size = 0U;
|
|||
|
attr.stack_mem = NULL; //栈内存指针
|
|||
|
attr.stack_size = 1024 * 4; //栈大小
|
|||
|
attr.priority = 25; //优先级
|
|||
|
|
|||
|
if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
|
|||
|
{
|
|||
|
printf("Fail to create thread!\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
APP_FEATURE_INIT(iic_entry);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// #include <stdio.h>
|
|||
|
// #include <string.h>
|
|||
|
// #include <unistd.h>
|
|||
|
|
|||
|
// #include "ohos_init.h"
|
|||
|
// #include "cmsis_os2.h"
|
|||
|
|
|||
|
// uint32_t exec1, exec2;
|
|||
|
|
|||
|
// /***** 定时器1 回调函数 *****/
|
|||
|
// void Timer1_Callback(void *arg)
|
|||
|
// {
|
|||
|
// (void)arg;
|
|||
|
// printf("This is BearPi Harmony Timer1_Callback!\r\n");
|
|||
|
// }
|
|||
|
|
|||
|
// /***** 定时器2 回调函数 *****/
|
|||
|
// void Timer2_Callback(void *arg)
|
|||
|
// {
|
|||
|
// (void)arg;
|
|||
|
// printf("This is BearPi Harmony Timer2_Callback!\r\n");
|
|||
|
// }
|
|||
|
|
|||
|
// /***** 定时器创建 *****/
|
|||
|
// static void Timer_example(void)
|
|||
|
// {
|
|||
|
// osTimerId_t id1, id2;
|
|||
|
// uint32_t timerDelay;
|
|||
|
// osStatus_t status;
|
|||
|
|
|||
|
// exec1 = 1U;
|
|||
|
// id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
|
|||
|
// if (id1 != NULL)
|
|||
|
// {
|
|||
|
|
|||
|
// printf(" id1 != NULL\r\n");
|
|||
|
// // Hi3861 1U=10ms,100U=1S
|
|||
|
// timerDelay = 100U;
|
|||
|
// printf(" start status1\r\n ");
|
|||
|
// status = osTimerStart(id1, timerDelay);
|
|||
|
// printf(" ******end status1\r\n ");
|
|||
|
// if (status != osOK)
|
|||
|
// {
|
|||
|
// // Timer could not be started
|
|||
|
// printf(" Timer could not be started\r\n ");
|
|||
|
// }else{
|
|||
|
// printf(" Timer could be started\r\n "); //have
|
|||
|
// }
|
|||
|
// printf(" end status1\r\n "); //have
|
|||
|
// }else{
|
|||
|
// printf(" id1 = NULL\r\n ");
|
|||
|
// }
|
|||
|
|
|||
|
// exec2 = 1U;
|
|||
|
// id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
|
|||
|
// if (id2 != NULL)
|
|||
|
// {
|
|||
|
|
|||
|
// printf(" id2 != NULL\r\n ");
|
|||
|
// // Hi3861 1U=10ms,300U=3S
|
|||
|
// timerDelay = 300U;
|
|||
|
|
|||
|
// printf(" start status2\r\n ");
|
|||
|
// status = osTimerStart(id2, timerDelay);
|
|||
|
// printf(" *******end status2\r\n ");
|
|||
|
// if (status != osOK)
|
|||
|
// {
|
|||
|
// // Timer could not be started
|
|||
|
// printf(" Timer could not be started \r\n");
|
|||
|
// }else{
|
|||
|
// printf(" Timer could be started\r\n ");
|
|||
|
// }
|
|||
|
// printf(" end status2\r\n ");
|
|||
|
// }else{
|
|||
|
// printf(" id2 = NULL\r\n ");
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// APP_FEATURE_INIT(Timer_example);
|
|||
|
|
|||
|
|
|||
|
//原文链接:https://blog.csdn.net/qq_36347513/article/details/117650058
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|