- added I2C DAC

git-svn-id: http://moon:8086/svn/projects/HendiControl@11 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-02-14 22:18:55 +00:00
parent 189e3454d9
commit 15df3f87ab
+95 -7
View File
@@ -110,6 +110,85 @@ void timer_init()
} }
enum I2C
{
I2C_START = 0x08,
I2C_REPEATED_START = 0x10,
I2C_SLA_ACK = 0x18,
I2C_SLA_NACK = 0x20,
I2C_DATA_ACK = 0x28,
I2C_DATA_NACK = 0x30,
I2C_LOST = 0x38,
I2C_READ = 0x01,
I2C_WRITE = 0x00
};
void i2c_init()
{
TWBR = 0xFF;
TWSR = 0x03;
}
void i2c_send(uint8_t addr, uint8_t rw, uint8_t *data, size_t size)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
// Send START condition
do
{
while (!(TWCR &(1<<TWINT))); // Wait for TWINT Flag set. This indicates
// that the START condition has been
// transmitted.
if((TWSR & 0xF8) != I2C_START)
{
printf("Error I2C_START\n");
break;
}
TWDR = addr<<1 | rw; // Load SLA_W into TWDR Register. Clear
// TWINT bit in TWCR to start transmission of
// address.
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR &(1<<TWINT))); // Wait for TWINT Flag set. This indicates
// that the SLA+W has been transmitted, and
// ACK/NACK has been received.
if ((TWSR & 0xF8) != I2C_SLA_ACK)
{
printf("Error I2C_SLA_ACK\n");
break;
}
// Check value of TWI Status Register. Mask
// prescaler bits. If status different from
// MT_SLA_ACK go to ERROR
for (size_t i=0; i < size; i++)
{
TWDR = *(data++);
TWCR = (1<<TWINT) | (1<<TWEN);
// Load DATA into TWDR Register. Clear
// TWINT bit in TWCR to start transmission of
// data.
while (!(TWCR & (1<<TWINT)));
// Wait for TWINT Flag set. This indicates
// that the DATA has been transmitted, and
// ACK/NACK has been received}
if ((TWSR & 0xF8) != I2C_DATA_ACK)
{
printf("Error I2C_DATA_ACK\n");
break;
}
// Check value of TWI Status Register. Mask
// prescaler bits. If status different from
// MT_DATA_ACK go to ERROR.
}
} while (0);
TWCR = (1<<TWINT)| (1<<TWEN)|(1<<TWSTO); // Transmit STOP condition.
}
void adc_init() void adc_init()
{ {
PRR &= ~(1 << PRADC); PRR &= ~(1 << PRADC);
@@ -125,8 +204,8 @@ void adc_start_conversion(int ch)
int adc_is_fininshed() int adc_is_fininshed()
{ {
return (int)(0 == (ADCSRA & (1 << ADSC))); return (int)(0 == (ADCSRA & (1 << ADSC)));
} }
int16_t adc_read() int16_t adc_read()
{ {
return adc_value; return adc_value;
@@ -177,6 +256,7 @@ void port_init()
} }
FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
int main(void) int main(void)
{ {
@@ -184,10 +264,14 @@ int main(void)
uart_init(); uart_init();
timer_init(); timer_init();
adc_init(); adc_init();
i2c_init();
stdout = &uart_file; stdout = &uart_file;
printf("UART test version 1.0\n\n"); printf("UART test version 1.0\n\n");
uint8_t write_dac_full[] = {0x40, 0x00, 0x00}; // 6.1 Write Volatile DAC Register (C2:C0 = 00x)
i2c_send(0x60, I2C_WRITE, write_dac_full, sizeof(write_dac_full));
sei(); sei();
adc_start_conversion(adc_ch); adc_start_conversion(adc_ch);
while (1) while (1)
@@ -196,15 +280,21 @@ int main(void)
if (msgCode) if (msgCode)
{ {
cli(); cli();
switch(msgCode) uint8_t _msgCode = msgCode;
uint8_t _msgData = msgData;
sei();
switch(_msgCode)
{ {
case Command: case Command:
printf("Command : %c\n", msgData); printf("Command : %c\n", _msgData);
uint8_t write_dac[] = {0x00+(_msgData-'0'), 0x00}; // 6.2 Write Volatile Memory (C2:C0 = 010)
i2c_send(0x60, I2C_WRITE, write_dac, sizeof(write_dac));
break; break;
case AdcComplete: case AdcComplete:
{ {
size_t ch = msgData; size_t ch = _msgData;
if (ch == 6) if (ch == 6)
{ {
printf("ADC=%d\n", adc_value[ch]); printf("ADC=%d\n", adc_value[ch]);
@@ -235,8 +325,6 @@ int main(void)
printf("Error!!!\n"); printf("Error!!!\n");
break; break;
} }
msgCode = NOP;
sei();
} }
} }
} }