Added serial output testing.

This commit is contained in:
Marvin Blum
2017-03-21 14:42:27 +01:00
parent c358ee6a05
commit 48cf07de97
3 changed files with 117 additions and 6 deletions

46
main.c
View File

@@ -2,11 +2,49 @@
#include <util/delay.h> #include <util/delay.h>
#include "pins.h" #include "pins.h"
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((16000000UL/(USART_BAUDRATE*16UL)))-1)
void loop();
int main(){ int main(){
digitalPinMode(6, INPUT);
digitalPinMode(7, INPUT);
digitalPinMode(12, OUTPUT);
// TODO verstehen und dann in Funktion packen
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
UBRR0H = BAUD_PRESCALE>>8;
UBRR0L = BAUD_PRESCALE;
while(1){ while(1){
digitalWrite(5, HIGH); loop();
_delay_ms(1000);
digitalWrite(5, LOW);
_delay_ms(1000);
} }
} }
void loop(){
/*if(DDRB&0x10){
digitalWrite(12, HIGH);
}
else{
digitalWrite(12, LOW);
}*/
// TODO verstehen und dann in Funktion packen
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = 'h';
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = 'e';
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = 'l';
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = 'l';
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = 'o';
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = '\n';
// while(!(SPSR&(1<<SPIF)));
_delay_ms(1000);
}

70
pins.c
View File

@@ -3,6 +3,50 @@
const unsigned char LOW = 0x00; const unsigned char LOW = 0x00;
const unsigned char HIGH = 0x01; const unsigned char HIGH = 0x01;
const unsigned char INPUT = 0x00;
const unsigned char OUTPUT = 0x01;
void digitalPinMode(unsigned char pin, unsigned char mode){
if(pin > 13){
return;
}
if(pin < 8){
if(mode == INPUT){
DDRD &= ~_BV(DDD0+pin);
}
else{
DDRD |= _BV(DDD0+pin);
}
}
else{
pin -= 8;
if(mode == INPUT){
DDRB &= ~_BV(DDB0+pin);
}
else{
DDRB |= _BV(DDB0+pin);
}
}
}
int digitalRead(unsigned char pin){
if(pin > 13){
return 0;
}
// TODO turn off pwm
if(pin < 8){
// pin 0-7
return PIND&_BV(DDD0+pin);
}
// pin 8-13
pin -= 8;
return PINB&_BV(DDB0+pin);
}
void digitalWrite(unsigned char pin, unsigned char value){ void digitalWrite(unsigned char pin, unsigned char value){
if(pin > 13){ if(pin > 13){
@@ -13,7 +57,7 @@ void digitalWrite(unsigned char pin, unsigned char value){
if(pin < 8){ if(pin < 8){
// pin 0-7 // pin 0-7
DDRD |= _BV(DDD0+pin); // DDRD |= _BV(DDD0+pin);
if(value == HIGH){ if(value == HIGH){
PORTD |= _BV(DDD0+pin); PORTD |= _BV(DDD0+pin);
@@ -24,7 +68,8 @@ void digitalWrite(unsigned char pin, unsigned char value){
} }
else{ else{
// pin 8-13 // pin 8-13
DDRB |= _BV(DDB0+pin); pin -= 8;
// DDRB |= _BV(DDB0+pin);
if(value == HIGH){ if(value == HIGH){
PORTB |= _BV(DDB0+pin); PORTB |= _BV(DDB0+pin);
@@ -35,6 +80,27 @@ void digitalWrite(unsigned char pin, unsigned char value){
} }
} }
void analogPinMode(unsigned char pin, unsigned char mode){
if(pin > 7){
return;
}
if(mode == INPUT){
DDRC &= ~_BV(DDC0+pin);
}
else{
DDRC |= _BV(DDC0+pin);
}
}
int analogRead(unsigned char pin){
if(pin > 7){
return 0;
}
return PINC&_BV(DDC0+pin);
}
void analogWrite(unsigned char pin, unsigned char value){ void analogWrite(unsigned char pin, unsigned char value){
if(pin > 7){ if(pin > 7){
return; return;

7
pins.h
View File

@@ -3,8 +3,15 @@
extern const unsigned char LOW; extern const unsigned char LOW;
extern const unsigned char HIGH; extern const unsigned char HIGH;
extern const unsigned char INPUT;
extern const unsigned char OUTPUT;
void digitalPinMode(unsigned char, unsigned char);
int digitalRead(unsigned char);
void digitalWrite(unsigned char, unsigned char); void digitalWrite(unsigned char, unsigned char);
void analogPinMode(unsigned char, unsigned char);
int analogRead(unsigned char);
void analogWrite(unsigned char, unsigned char); void analogWrite(unsigned char, unsigned char);
#endif #endif