Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * opdracht2c.c
- *
- * Created: 17/02/2018 20:27:52
- * Author : Willem
- */
- #define F_CPU 16000000
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
- char leesNummer();
- void binaryDisplay(int x);
- ISR(USART_RXC_vect);
- int teller = 0;
- void writeChar(char x);
- void writeString(char st[]);
- void writeInt(int i);
- int main(void) {
- sei();
- UCSR0B = (1 << TXEN0); // Enable de USART Transmitter
- UCSR0C |= (1 << UCSZ01 | 1 << UPM01) ; /* 7 data bits, */
- UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
- UBRR0H=00;
- UBRR0L=103; //baudrade 9600 bij
- DDRB = 0b00011111;
- while (1) {
- for(uint8_t i = 0; i <= 1; i++){
- if(i == 0){
- PORTB &= ~(1 << 4);
- _delay_ms(1000);
- writeInt(teller);
- }
- else if(i == 1){
- if(teller > 0){
- PORTB &= ~(1 << 3);
- PORTB &= ~(1 << 2);
- PORTB &= ~(1 << 1);
- PORTB &= ~(1 << 0);
- binaryDisplay(teller);
- teller = teller - 1;
- PORTB |= (1 << 4);
- i = -1;
- }
- _delay_ms(1000);
- }
- }
- PORTB &= ~(1 << 3);
- PORTB &= ~(1 << 2);
- PORTB &= ~(1 << 1);
- PORTB &= ~(1 << 0);
- }
- return (0);
- }
- void binaryDisplay(int x){
- if(x >= 8){
- PORTB |= (1 << 3);
- x = x - 8;
- }
- if(x >= 4){
- PORTB |= (1 << 2);
- x = x - 4;
- }
- if(x >= 2){
- PORTB |= (1 << 1);
- x = x - 2;
- }
- if(x >= 1){
- PORTB |= (1 << 0);
- x = x - 1;
- }
- }
- char leesNummer() {
- while(!(UCSR0A & (1<<RXC0)));
- return UDR0;
- }
- int hexToInt(char c) {
- char hex[] = "0123456789ABCDEF"; // de HEX waarden
- int output = 0; // de output variable
- for(int i = 0; i <= 15; i++) { // loop door de hex char array
- if(hex[i] == c) { output = i; } // als de hex waarde gelijk is aan de ingegeven waarde is dat de output
- }
- return output; // return de output
- }
- ISR(USART_RX_vect){
- char c = leesNummer();
- teller = hexToInt(c);
- }
- void writeChar(char x) {
- while(~UCSR0A & (1 << UDRE0));
- UDR0 = x;
- }
- void writeString(char st[]) {
- for(uint8_t i = 0 ; st[i] != 0 ; i++) {
- writeChar( st[i] );
- }
- }
- void writeInt(int i) {
- char buffer[8];
- itoa(i,buffer,10);
- writeString(buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement