Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <util/delay.h>
- #include <util/twi.h>
- #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
- #define CPU_16MHz 0x00
- #define CPU_8MHz 0x01
- #define CPU_4MHz 0x02
- #define CPU_2MHz 0x03
- #define CPU_1MHz 0x04
- #define CPU_500kHz 0x05
- #define CPU_250kHz 0x06
- #define CPU_125kHz 0x07
- #define CPU_62kHz 0x08
- #define SUCCESS 1
- #define ERROR 0
- void TWI_init(void)
- {
- //set SCL frequency
- TWSR = 0x01;
- TWBR = 0x0C;
- //enable TWI
- TWCR = (1<<TWEN);
- }
- void TWI_start(void)
- {
- TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
- while ((TWCR & (1<<TWINT)) == 0);
- }
- void TWI_stop(void)
- {
- TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
- }
- void TWI_put_char(uint8_t u8data)
- {
- TWDR = u8data;
- TWCR = (1<<TWINT)|(1<<TWEN);
- while ((TWCR & (1<<TWINT)) == 0);
- }
- uint8_t TWI_get_status(void)
- {
- uint8_t status;
- //mask status
- status = TWSR & 0xF8;
- return status;
- }
- uint8_t serial_7seg_write_char(uint8_t data)
- {
- TWI_start();
- if (TWI_get_status() != 0x08)
- return ERROR;
- // send the address
- TWI_put_char(0xE2);
- if (TWI_get_status() != 0x28)
- return ERROR;
- // write one bye
- TWI_put_char(data);
- if (TWI_get_status() != 0x28)
- return ERROR;
- TWI_stop();
- return SUCCESS;
- }
- uint8_t serial_7seg_move_cursor()
- {
- return serial_7seg_write_char(0x79);
- }
- uint8_t serial_7seg_clear_display()
- {
- return serial_7seg_write_char(0x76);
- }
- int main(void)
- {
- // set for 16 MHz clock
- CPU_PRESCALE(CPU_16MHz);
- TWI_init();
- DDRB &= ~(1 << PB7);
- while (1)
- {
- if (PINB & (1<<PB7))
- {
- _delay_ms(100);
- if (PINB & (1<<PB7))
- {
- serial_7seg_write_char('s');
- serial_7seg_write_char('e');
- serial_7seg_write_char('n');
- serial_7seg_write_char('d');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement