Celem jest zapisanie w pamięci AT24C04 jakiś danych.
Program w Bascomie:
Zapisuje do adresu 1 wartość 100 i wyświetla ją na LCD.
Program w C:
No i w efekcie wywala wszystkie możliwe błędy... Kod ten jest na żywca ściągnięty z pdfa o ATMEGA16 i nie działa :/
Program w Bascomie:
$regfile = "m16def.dat"
$crystal = 4000000
Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Porta.3 , Rs = Porta.2
Config Lcd = 20 * 4
Dim Temp As Byte
Cls
Cursor On Blink
I2cstart
I2cwbyte 162
I2cwbyte 1
I2cwbyte 100
I2cstop
Waitms 15
I2cstart
I2cwbyte 162
I2cwbyte 1
I2cstart
I2cwbyte 163
I2crbyte Temp , 9
I2cstop
Lcd "Temp: " ; Temp
End
Zapisuje do adresu 1 wartość 100 i wyświetla ją na LCD.
Program w C:
#define SCL_CLOCK 100000L
#define F_CPU 4000000
#include <util/twi.h>
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char SLA_W = 162, DATA1 = 1, DATA2 = 100;
DDRA=0xFF;
PORTA =0xFF;
LCDinit();
LCDcls();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
LCDstr("START");
TWDR = SLA_W; //zapis adresu
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
LCDstr("MT_SLA_ACK");
TWDR = DATA1; //zapis komórki
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
LCDstr("MT_DATA_ACK");
TWDR = DATA2; //zapis danych
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
LCDstr("MT_DATA_ACK");
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
No i w efekcie wywala wszystkie możliwe błędy... Kod ten jest na żywca ściągnięty z pdfa o ATMEGA16 i nie działa :/
