334 lines
6.8 KiB
C
334 lines
6.8 KiB
C
![]() |
#include <stdio.h>
|
|||
|
#include "time.h"
|
|||
|
#include "los_swtmr.h"
|
|||
|
#include "los_sys.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>
|
|||
|
|
|||
|
|
|||
|
// #ifdef __cplusplus
|
|||
|
// #if __cplusplus
|
|||
|
// extern "C" {
|
|||
|
// #endif /* __cplusplus */
|
|||
|
// #endif /* __cplusplus */
|
|||
|
|
|||
|
|
|||
|
#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
|
|||
|
|
|||
|
|
|||
|
typedef unsigned int u16; //对数据类型进行声明定义
|
|||
|
typedef unsigned char u8;
|
|||
|
|
|||
|
|
|||
|
UINT32 g_timerCount1 = 0;
|
|||
|
UINT32 g_timerCount2 = 0;
|
|||
|
|
|||
|
// VOID Timer1_CallBack(UINT32 arg)
|
|||
|
// {
|
|||
|
// UINT64 lastTick;
|
|||
|
|
|||
|
// g_timerCount1++;
|
|||
|
// lastTick=(UINT32)LOS_TickCountGet();
|
|||
|
// // dprintf("g_timerCount1=%d\n", g_timerCount1);
|
|||
|
// // dprintf("tick_last1=%d\n", lastTick);
|
|||
|
// }
|
|||
|
|
|||
|
// VOID Timer2_CallBack(UINT32 arg)
|
|||
|
// {
|
|||
|
// UINT64 lastTick;
|
|||
|
|
|||
|
// lastTick=(UINT32)LOS_TickCountGet();
|
|||
|
// g_timerCount2++;
|
|||
|
// // dprintf("g_timerCount2=%d\n", g_timerCount2);
|
|||
|
// // dprintf("tick_last2=%d\n", lastTick);
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/*延时函数*/
|
|||
|
void delayab(u16 i)
|
|||
|
{
|
|||
|
while(i--);
|
|||
|
}
|
|||
|
|
|||
|
void delaynm(unsigned int z)
|
|||
|
{
|
|||
|
unsigned int x,y;
|
|||
|
for(x=z;x>0;x--)
|
|||
|
for(y=120;y>0;y--);
|
|||
|
}
|
|||
|
|
|||
|
void delay1(void)
|
|||
|
{
|
|||
|
unsigned char i,j;
|
|||
|
for(i=10;i>0;i--)
|
|||
|
for(j=248;j>0;j--);
|
|||
|
}
|
|||
|
|
|||
|
void delay2(void)
|
|||
|
{
|
|||
|
unsigned char i,j,k;
|
|||
|
for(k=10;k>0;k--)
|
|||
|
for(i=20;i>0;i--)
|
|||
|
for(j=248;j>0;j--);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void iic(void* args ){
|
|||
|
int i,j;
|
|||
|
|
|||
|
UINT16 id1; // Timer1 id
|
|||
|
UINT16 id2; // Timer2 id
|
|||
|
UINT32 tick;
|
|||
|
|
|||
|
// LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
|
|||
|
// LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
|
|||
|
// LOS_SwtmrCreate(200, LOS_SWTMR_MODE_PERIOD, Timer1_CallBack, &id1, NULL); //时间的单位是ms
|
|||
|
// LOS_SwtmrCreate(80, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, NULL);
|
|||
|
// dprintf("create Timer1 success\n");
|
|||
|
|
|||
|
|
|||
|
|
|||
|
printf("iic thread running...\n");
|
|||
|
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);
|
|||
|
|
|||
|
// LOS_SwtmrStart(id1);
|
|||
|
// dprintf("start Timer1 sucess\n");
|
|||
|
// //LOS_TaskDelay(200);
|
|||
|
// LOS_TaskDelay(200);
|
|||
|
// LOS_SwtmrTimeGet(id1, &tick);
|
|||
|
// dprintf("tick = %d\n", tick);
|
|||
|
// LOS_SwtmrStop(id1);
|
|||
|
// dprintf("stop Timer1 sucess\n");
|
|||
|
// // LOS_SwtmrStart(id1);
|
|||
|
// // LOS_TaskDelay(1000);
|
|||
|
// LOS_SwtmrDelete(id1);
|
|||
|
// dprintf("delete Timer1 sucess\n");
|
|||
|
|
|||
|
|
|||
|
|
|||
|
write_iic(OUT0);
|
|||
|
write_iic(OUT1);
|
|||
|
//usleep(1000*1000);
|
|||
|
usleep(100);
|
|||
|
int a=0;
|
|||
|
|
|||
|
// LOS_SwtmrStart(id2);
|
|||
|
// dprintf("start Timer2 sucess\n");
|
|||
|
// //LOS_TaskDelay(1000);
|
|||
|
// LOS_TaskDelay(200);
|
|||
|
// LOS_SwtmrTimeGet(id2, &tick);
|
|||
|
// dprintf("tick = %d\n", tick);
|
|||
|
// LOS_SwtmrStop(id2);
|
|||
|
// dprintf("stop Timer2 sucess\n");
|
|||
|
// LOS_SwtmrDelete(id2);
|
|||
|
// dprintf("delete Timer2 sucess\n");
|
|||
|
// LOS_SwtmrStop(id2);
|
|||
|
// LOS_SwtmrDelete(id2);
|
|||
|
// write_data(0b11111110,0b11111111);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// write_data(0b11111101,0b11111000);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// write_data(0b00100000,0b00000000);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// write_data(0b00100000,0b00000000);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// write_data(0b00010000,0b11111111);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
for(int a=0;a<3;a++){printf("+++a:%d\r\n",a);}
|
|||
|
//delay2();
|
|||
|
}
|
|||
|
|
|||
|
printf("set count :%d\r\n",a=a+1);
|
|||
|
|
|||
|
//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);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// /***** 定时器创建 *****/
|
|||
|
// static void Timer_example(void)
|
|||
|
// {
|
|||
|
// UINT16 id1; // Timer1 id
|
|||
|
// UINT16 id2; // Timer2 id
|
|||
|
// UINT32 tick;
|
|||
|
|
|||
|
// LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
|
|||
|
// LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
|
|||
|
// dprintf("create Timer1 success\n");
|
|||
|
|
|||
|
// LOS_SwtmrStart(id1);
|
|||
|
// dprintf("start Timer1 sucess\n");
|
|||
|
// LOS_TaskDelay(200);
|
|||
|
// LOS_SwtmrTimeGet(id1, &tick);
|
|||
|
// dprintf("tick =%d\n", tick);
|
|||
|
// LOS_SwtmrStop(id1);
|
|||
|
// dprintf("stop Timer1 sucess\n");
|
|||
|
|
|||
|
// LOS_SwtmrStart(id1);
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// LOS_SwtmrDelete(id1);
|
|||
|
// dprintf("delete Timer1 sucess\n");
|
|||
|
|
|||
|
// LOS_SwtmrStart(id2);
|
|||
|
// dprintf("start Timer2\n");
|
|||
|
// LOS_TaskDelay(1000);
|
|||
|
// LOS_SwtmrStop(id2);
|
|||
|
// LOS_SwtmrDelete(id2);
|
|||
|
|
|||
|
// }
|
|||
|
|
|||
|
// APP_FEATURE_INIT(Timer_example);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|