pwm begonnen zu implementieren, geht aber mal gar nicht. Muss mitm Oszi gecheckt werden
This commit is contained in:
@@ -12,6 +12,11 @@
|
||||
#define LDR_DDR DDRC
|
||||
#define LDR_PIN PC2
|
||||
|
||||
#define LDR_CHANNEL 2
|
||||
#define LDR_CHANNEL 3
|
||||
#define LDR_SAMPLES 20
|
||||
#define LDR_PRESCALER 4
|
||||
|
||||
void LDR_init(void);
|
||||
uint8_t LDR_get_value(void);
|
||||
|
||||
#endif /* INCLUDE_LDR_H_ */
|
||||
|
||||
@@ -8,8 +8,14 @@
|
||||
#ifndef INCLUDE_MATRIX_H_
|
||||
#define INCLUDE_MATRIX_H_
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include "woerter.h"
|
||||
|
||||
const uint8_t log_lookup[64] PROGMEM =
|
||||
{0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 12, 13, 15, 16, 17, 19, 21, 23,
|
||||
25, 27, 29, 32, 35, 38, 41, 45, 49, 54, 59, 64, 70, 76, 83, 90, 98, 107, 117, 128, 139, 152, 165, 180, 197, 214, 234, 255};
|
||||
|
||||
|
||||
uint8_t qlock_matrix[MAX_COLS][MAX_ROWS];
|
||||
|
||||
|
||||
|
||||
27
Firmware/include/pwm.h
Normal file
27
Firmware/include/pwm.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* pwm.h
|
||||
*
|
||||
* Created on: 31.01.2016
|
||||
* Author: BlexTw11
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_PWM_H_
|
||||
#define INCLUDE_PWM_H_
|
||||
|
||||
#include "global.h"
|
||||
|
||||
#define PWM_ISR ISR
|
||||
#define PWM_TIMER TIMER1_COMPA_vect
|
||||
|
||||
#define F_PWM 100L // PWM-Frequenz in Hz
|
||||
#define PWM_PRESCALER 8 // Vorteiler für den Timer
|
||||
#define PWM_STEPS 256 // PWM-Schritte pro Zyklus(1..256)
|
||||
#define PWM_COMPARE_VAL 2816
|
||||
|
||||
#define PWM_FACTOR PWM_COMPARE_VAL / PWM_STEPS
|
||||
|
||||
void PWM_init_timer(void);
|
||||
void PWM_update(void);
|
||||
bool PWM_get_status(void);
|
||||
|
||||
#endif /* INCLUDE_PWM_H_ */
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#define UART_MAXSTRLEN 20
|
||||
|
||||
void UART_init(uint16_t baud_rate, char buffer[]);
|
||||
void UART_init(uint16_t baud_rate, char * buffer);
|
||||
|
||||
uint8_t UART_rx_char(void);
|
||||
uint8_t UART_rx_str(void);
|
||||
|
||||
@@ -13,3 +13,9 @@ void LDR_init(void)
|
||||
{
|
||||
ADC_init();
|
||||
}
|
||||
|
||||
uint8_t LDR_get_value(void)
|
||||
{
|
||||
// For values between 0-255 we divide the ADC-value (0-1023) through 4.
|
||||
return ADC_read_avg(LDR_CHANNEL, LDR_SAMPLES)/LDR_PRESCALER;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "ds1307.h"
|
||||
#include "adc.h"
|
||||
#include "ldr.h"
|
||||
#include "pwm.h"
|
||||
|
||||
volatile uint8_t uart_str_complete = 0;
|
||||
volatile char uart_buffer[UART_MAXSTRLEN + 1] = "";
|
||||
@@ -19,10 +20,11 @@ volatile uint8_t flag_1sec;
|
||||
|
||||
ISR(TIMER0_COMPA_vect)
|
||||
{
|
||||
|
||||
static uint8_t millisekunden = 0;
|
||||
static uint8_t sekunden = 0;
|
||||
|
||||
if (millisekunden++ == 50)
|
||||
if (millisekunden++ == 5)
|
||||
{
|
||||
//TOGL(PORTC, PC0);
|
||||
millisekunden = 0;
|
||||
@@ -46,22 +48,10 @@ ISR(USART_RX_vect)
|
||||
void init_timer0(void)
|
||||
{
|
||||
// Timer0 initialisieren.
|
||||
TCCR0A = (1 << WGM01); // CTC Modus
|
||||
TCCR0B = (1 << CS02); // Prescaler: 256
|
||||
OCR0A = 72-1; // Zaehlwert auf 73 setzen
|
||||
TCCR0A = (1 << WGM01); // CTC Modus (Clear Timer on Compare)
|
||||
TCCR0B = (1 << CS02 | 1 << CS00); // Prescaler: 1024
|
||||
OCR0A = 180-1; // Zaehlwert auf 180 setzen
|
||||
TIMSK0 |= (1 << OCIE0A); // Timer Interrupt Maske aktivieren
|
||||
|
||||
/*
|
||||
// Timer initialisieren.
|
||||
TCCR1B = (1 << CS12) | (1 << WGM12); // Prescaler auf 256
|
||||
|
||||
//TCNT0 = 0;
|
||||
|
||||
OCR1AL = 72-1;
|
||||
OCR1AH = 0;
|
||||
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
*/
|
||||
}
|
||||
|
||||
int8_t UART_bla(void)
|
||||
@@ -88,10 +78,11 @@ int main(void)
|
||||
flag_50ms = 0;
|
||||
flag_1sec = 0;
|
||||
|
||||
init_timer0();
|
||||
//init_timer0();
|
||||
PWM_init_timer();
|
||||
//shiftReg_Init();
|
||||
ADC_init();
|
||||
UART_init(BAUD_RATE, uart_buffer);
|
||||
UART_init(BAUD_RATE, (char *) uart_buffer);
|
||||
|
||||
LED_DDR = (1 << LED_PIN);
|
||||
LED_PORT &= ~(1 << LED_PIN);
|
||||
@@ -129,26 +120,24 @@ int main(void)
|
||||
}
|
||||
UART_tx_char('\n');
|
||||
*/
|
||||
uint16_t ldr;
|
||||
uint8_t status;
|
||||
while(1)
|
||||
{
|
||||
|
||||
if (flag_1sec)
|
||||
{
|
||||
flag_1sec = 0;
|
||||
|
||||
//shiftReg_Transmit(led);
|
||||
|
||||
//TOGL(LED_PORT, LED_PIN);
|
||||
}
|
||||
|
||||
ldr = ADC_read_avg(LDR_CHANNEL, 20);
|
||||
|
||||
UART_tx_char(ldr / 100);
|
||||
UART_tx_char(ldr % 100);
|
||||
PWM_update();
|
||||
status = PWM_get_status();
|
||||
/*
|
||||
if (status)
|
||||
SET(LED_PORT, LED_PIN);
|
||||
else
|
||||
CLR(LED_PORT, LED_PIN);
|
||||
*/
|
||||
/*
|
||||
UART_tx_char((ldr / 100) & 0xff);
|
||||
UART_tx_char((ldr % 100) & 0xff);
|
||||
UART_tx_char('\n');
|
||||
|
||||
_delay_ms(100);
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
69
Firmware/src/pwm.c
Normal file
69
Firmware/src/pwm.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* pwm.c
|
||||
*
|
||||
* Created on: 31.01.2016
|
||||
* Author: BlexTw11
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "uart.h"
|
||||
#include "ldr.h"
|
||||
#include "pwm.h"
|
||||
// Defines an den Controller und die Anwendung anpassen
|
||||
|
||||
uint16_t timer_edge = 0;
|
||||
volatile bool on_not_off = TRUE;
|
||||
|
||||
|
||||
PWM_ISR(PWM_TIMER)
|
||||
{
|
||||
/*
|
||||
UART_tx_char((OCR1A / 100) & 0xff);
|
||||
UART_tx_char((OCR1A % 100) & 0xff);
|
||||
UART_tx_char('\r');
|
||||
UART_tx_char('\n');
|
||||
*/
|
||||
TOGL(LED_PORT, LED_PIN);
|
||||
if (on_not_off)
|
||||
{
|
||||
OCR1AH = (timer_edge >> 8) & 0xff;
|
||||
OCR1AL = timer_edge & 0xff;
|
||||
on_not_off = FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
OCR1AH = (PWM_COMPARE_VAL >> 8) & 0xff;
|
||||
OCR1AL = PWM_COMPARE_VAL & 0xff;
|
||||
on_not_off = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
void PWM_init_timer(void)
|
||||
{
|
||||
// Timer1 initialisieren.
|
||||
TCCR1B |= (1 << CS11) | (1 << CS10); // Prescaler: 64
|
||||
|
||||
uint8_t ldr_value = LDR_get_value() * (uint16_t) PWM_FACTOR;
|
||||
OCR1AH = (ldr_value >> 8) & 0xff;
|
||||
OCR1AL = ldr_value & 0xff;
|
||||
TIMSK1 |= (1 << OCIE1A); // Timer Interrupt Maske aktivieren
|
||||
}
|
||||
|
||||
|
||||
void PWM_update(void)
|
||||
{
|
||||
uint8_t ldr_value = LDR_get_value();
|
||||
|
||||
timer_edge = ldr_value * (uint16_t) PWM_FACTOR;
|
||||
}
|
||||
|
||||
bool PWM_get_status(void)
|
||||
{
|
||||
return on_not_off;
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
volatile char * uart_buffer;
|
||||
|
||||
void UART_init(uint16_t baud_rate, char buffer[])
|
||||
void UART_init(uint16_t baud_rate, char * buffer)
|
||||
{
|
||||
// Set baud rate (Calculation type is VERY importent!)
|
||||
uint16_t baud_val = ( F_CPU/16/baud_rate - 1 );
|
||||
|
||||
Reference in New Issue
Block a user