Skip to content
Snippets Groups Projects
Commit 39ccc194 authored by Julia Scharsich's avatar Julia Scharsich
Browse files

Delete lay1+2 copy.c

parent 05a68801
No related branches found
No related tags found
No related merge requests found
#define F_CPU 12000000UL
#define BAUD 9600
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/setbaud.h>
#include <util/atomic.h>
#include "julart.h" // Uart Functions
#include "crc.h" //crc implementation
#define ATOMIC_BLOCK(type)
#define ATOMIC_RESTORESTATE
#define ATOMIC_FORCEON
typedef unsigned char byte;
typedef uint32_t uint32;
#define MAXFRAMESIZE 257 //in byte
// Init of Send-/Receive-Buffer
byte send[MAXFRAMESIZE]= {0}; // 0xB3, 0x10 = 10110011, 00010000
byte frame[MAXFRAMESIZE]; // 0 - Preambel, 1 to 4 - CRC checksum, 5 - size, 6 to n (n = max. 256) - Payload
byte flag = 1;
byte bufferIndex = 0; // Zählvariable für ISR
byte sendIndex = 0;
byte bitIndex = 7;
byte readyToReceive = 0;
byte readyToSend = 0;
//frame = {0b01111110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b10110011};
void delay(int ms){
while(ms>0){
_delay_ms(1);
ms--;
}
return;
}
void fillSend (byte* payload, byte size){
//payload padden (Preambel, crc berechnung, size, rückgabe, readyToSend)
send[0] = 0b01111110;
uint32_t checksum = crc_comp(payload, size);
for(int i = 1; i < 5; i++){
byte x = 32-i*8;
send[i] = (byte)((checksum & ((uint32_t)(255) << (x))) >> x);
}
send[5] = size;
for(int i = 6; i < size+5; i++){
send[i]= payload[i-6];
}
readyToSend = 1;
}
/* Funktionsdeklarationen für Interruptsetups */
void setup_Timer();
void setup_PinC();
int main(void){
uart_init();
//Init Input-/Output-Stream
stdout = &uart_io;
stdin = &uart_io;
//Data Direction
DDRD = 0x0;
//PORTD = 0x10;
DDRB = 0x38; //(00111000) --> output at B3, B4, B5
/* Setup Interrupt */
setup_Timer();
setup_PinC();
sei(); //set SREG (one)
byte receiveIndex = 0;
byte receivedFrame[MAXFRAMESIZE];
byte sizeToReceive = MAXFRAMESIZE;
while(1){
if(!readyToSend){
byte sizeToSend = 2;
byte* payload = malloc(sizeToSend*sizeof(byte));
payload[0] = 0xB3;
payload[1] = 0x10;
ATOMIC_BLOCK(ATOMIC_FORCEON){
fillSend(payload, size);
}
}
ATOMIC_BLOCK(ATOMIC_FORCEON){
if(readyToReceive){
switch(receiveIndex){
case 0:
if(frame[receiveIndex] != 0x7E) break;
case 5:
sizeToReceive = receivedFrame[5];
receivedFrame = realloc (receivedFrame, sizeToReceive*sizeof(byte));
case (sizeToReceive-1):
if(!crc_check(receivedFrame, sizeToReceive)){
puts("Fehler bei der Übertragung durch CRC-Check festgestellt");
}
else{
puts("Übertragung erfolgreich abgeschlossen.");
//Hier evtl. noch Speicherung und/oder Ausgabe einbauen.
}
receiveIndex = 0;
break;
default:
if(bufferIndex != 0) receivedFrame[receiveIndex] = frame[bufferIndex-1];
else receivedFrame[receiveIndex] = frame[sizeToReceive-1];
receiveIndex++;
}
readyToReceive = 0;
printf("Received: %d\n",receivedFrame[receiveIndex] );
}
}
}
return 0;
}
/*
* Interrupt Service Routine using 16-Bit Timer
*/
ISR(TIMER1_COMPA_vect){
//Wenn flag gesetzt, sende Daten.
if(flag){
if(readyToSend){
// i-te Ziffer vom Sendebuffer wird extrahiert
byte bit = (send[sendIndex] & (1 << bitIndex)) >> bitIndex;
if(bit) {
PORTB |= (1 << PB5); //PB5 im PORTB setzen ()
PORTB |= (1 << PB3); //PB3 setzen -> LED an
}
else {
PORTB &= ~(1 << PB5); //PB5 im PORTB loeschen
PORTB &= ~(1 << PB3); //PB3 löschen -> LED aus
}
if(bitIndex==0) sendIndex++;
if(sendIndex==send[5]+5) sendIndex=0;
}
}
else PORTB ^= (1 << PB4); //Clock Signal -> Pin Change Interrupt
flag^=1;
}
/*
* Interrupt Service Routine using Pin Change
* PCINT2 covers PCINT[16...23]
* PCINT20 -- PD4
*/
ISR(PCINT2_vect){
frame[bufferIndex] = (frame[bufferIndex] << 1) + ((PIND & (1 << PD5)) >> PD5);
bitIndex--;
if(bitIndex == 255) {
if (bufferIndex == 0 && frame[bufferIndex] != 0x7E);
else {
if(bufferIndex != MAXFRAMESIZE-1) bufferIndex++;
else bufferIndex = 0;
readyToReceive = 1;
}
bitIndex=7;
}
//frame[bufferIndex] = (frame[bufferIndex] << 1) + ((PIND & (1 << PD5)) >> PD5);
//received |= (PIND & (1 << PD5)) << bitIndex;
//received = (received << 1) + ((PIND & ( 1 << PD5)) >> PD5);
}
/* Funktionen für das Einrichten von Interrupts */
/* Timer Interrupt */
void setup_Timer(){
/* Output Compare Register
* --> highest value can be FFFF because we have 16 bit available
*
* OCRn = [ (clock_speed / Prescaler_value) * Desired_time_in_Seconds ] - 1
* = 12000000/256 * 0.5 - 1
* = 23436 (DEC)
* = 5B8C(HEX)
*/
OCR1A = 0x5B8C;
// If TCNTn == OCR1A a timer overflow occurs which can trigger an interrupt.
TCCR1B |= (1 << WGM12);
// Timer/Counter Interrupt Mask Register - set on compare match
TIMSK1 |= (1 << OCIE1A);
//Set prescaler to 256
TCCR1B |= (1 << CS12);
}
/* Pin Change Interrupt */
void setup_PinC(){
// Enable checking PCMSK2
PCICR |= (1 << PCIE2);
// Clear Pin Change Interrupt Flag
PCIFR |= 0x7;
//Pin Change Interrupt enabled for Pin D4
PCMSK2 |= (1 <<PCINT20);
}
/*
* Quellen:
* ========
* [1] https://appelsiini.net/2011/simple-usart-with-avr-libc/
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment