-
Notifications
You must be signed in to change notification settings - Fork 12
/
usart-lib-tes.c
108 lines (89 loc) · 2.52 KB
/
usart-lib-tes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* usart_lib_tes.c
*
* Created: 8/17/2014 7:13:53 PM
* Author: EX4
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "lib/usart_ex4/usart_lib.h"
#include "lib/i2c_master/I2C_master.h"
#define i2c_dev_addr 0x20
/**
* i2c multi read
* @author
* @version
* @param
* @return
*/
void i2c_read(const uint8_t i2c_device_addr,const uint8_t i2c_register_addr, uint8_t *i2c_data_buffer,const uint8_t i2c_data_len)
{
uint8_t i;
I2C_start(i2c_device_addr);
I2C_write(i2c_register_addr);
I2C_start(i2c_device_addr+I2C_READ);
for (i = 0; i < i2c_data_len; i++)
{
*(i2c_data_buffer++) = (i<(i2c_data_len-1)) ? I2C_read_ack():I2C_read_nack();
}
I2C_stop();
}
void i2c_write(const uint8_t i2c_device_addr,const uint8_t i2c_register_addr, uint8_t *i2c_data_buffer,const uint8_t i2c_data_len)
{
uint8_t i;
I2C_start(i2c_device_addr);
I2C_write(i2c_register_addr);
for (i = 0; i < i2c_data_len; i++)
{
I2C_write(*(i2c_data_buffer++));
}
I2C_stop();
}
int main(void)
{
uint8_t xx = 0, yy = 0, i2c_buffer[32];
USART_InitFast(BAUD_57600, COM_CH0);
USART_SetStdin();
USART_SetStdout();
//printf_P(PSTR("HELLO WORLD\rEnter a number :"));
//scanf_P(PSTR("%u"), &xx);
//printf_P(PSTR("You enter : %u\r"), xx);
I2C_init();
sei();
_delay_ms(500);
//update pwm all to 50% dc
i2c_read(i2c_dev_addr,0x0c,&i2c_buffer[0],12);
i2c_buffer[0] = 1;
i2c_buffer[2] = i2c_buffer[3] = i2c_buffer[10] = i2c_buffer[11] = 128;
i2c_buffer[5] = i2c_buffer[7] = 216;
i2c_buffer[6] = i2c_buffer[8] = 0;
i2c_write(i2c_dev_addr,0x0d,&i2c_buffer[1],11);
i2c_write(i2c_dev_addr,0x0c,&i2c_buffer[0],1);
i2c_read(i2c_dev_addr,0x0c,&i2c_buffer[0],12);
printf_P(PSTR("%02x %02x %02x\r"), i2c_buffer[1], i2c_buffer[2], i2c_buffer[3]);
printf_P(PSTR("%02x %02x %02x\r"), i2c_buffer[4], i2c_buffer[5], i2c_buffer[7]);
printf_P(PSTR("%02x %02x %02x\r"), i2c_buffer[9], i2c_buffer[10], i2c_buffer[11]);
for (;;)
{
//read i2c dev DI
i2c_read(i2c_dev_addr,0x02,&i2c_buffer[0],1);
xx = i2c_buffer[0];
printf_P(PSTR("%02x\r"), i2c_buffer[0]);
i2c_write(i2c_dev_addr,0x03,&i2c_buffer[0],1);
//read i2c adc
i2c_read(i2c_dev_addr,0x0a,&i2c_buffer[0],2);
printf_P(PSTR("adc = %4d\r"), i2c_buffer[0] + (i2c_buffer[1]<<8));
//update pwm
if (xx!=yy)
{
yy=xx;
i2c_read(i2c_dev_addr,0x0c,&i2c_buffer[0],12);
i2c_buffer[0] = 1;
i2c_buffer[1] = i2c_buffer[4] = i2c_buffer[9] = yy;
i2c_write(i2c_dev_addr,0x0d,&i2c_buffer[1],11);
i2c_write(i2c_dev_addr,0x0c,&i2c_buffer[0],1);
}
_delay_ms(500);
}
}