source: projects/punch-card/driver/documation-m200/src/main.c @ 51

Last change on this file since 51 was 51, checked in by sven, 10 years ago

Sorted the sources a bit:

  • moved all the interrupt and card ring buffer related things to driver.c
  • renamed io.c (stupid name) to protocol.c (good name)
File size: 3.7 KB
Line 
1/**
2 * AVR M200 Punch card reader controller
3 * Main file
4 *
5 * This file contains the main() routine, initializes all systems (stdio,
6 * UART, ports, etc.) and calls the main loop in protocol.c
7 *
8 * - protocol.{h, c} implements the communication to the PC
9 * - punchcard.{h, c} implements the card model
10 * - driver.{h, c} implements the real device driver (ISR routines and ring buffer)
11 *
12 * The complete program is very specific to the wiring of the microcontroller,
13 * as defined in wiring.h. It was developed for an AVR ATmega 644 with a 8Mhz
14 * clock.
15 *
16 * The uC is set on an Olimex AVR-P40 board, having the TX pad connected to
17 * PD1 (pin15) and RX pad with PD0 (pin14).
18 *
19 * It was compiled with AVRStudio + WinAVR (gcc 4, avr-libc required).
20 *
21 * This file is part of the Punched Paper Project - Punch Card Device Drivers
22 * Copyright (C) 2009  Sven Koeppel
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 3 of
27 * the License, or (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, see
36 * <http://www.gnu.org/licenses/>.
37 **/
38
39#define __AVR_ATmega644__       1
40#define OSCSPEED        8000000         /* in Hz */
41
42#include <string.h>
43#include <stdio.h> // testweise
44
45#include "avr/io.h"
46#include "avr/interrupt.h"
47
48#include "punchcard.h"
49#include "wiring.h"
50#include "protocol.h"
51#include "driver.h"
52
53
54static FILE uart = FDEV_SETUP_STREAM(uart_transmit, NULL, _FDEV_SETUP_WRITE);
55
56/**
57 ** UART, I/O, LED
58 **/
59
60
61void Init(void)
62{
63        // Datenports einrichten
64
65    PORTA = 0x00; // erste 8 Daten-Input
66        PORTB = 0x00; // LED (auslassen, an=0x01), 3 Fehler (IN), Button (IN), ICSP
67        PORTC = 0x00; // 4 Daten, 3 Status (IN), 1 Signal (OUT)
68        PORTD = 0x00; // UART und -Flusssteuerung
69
70        DDRA = 0x00; // 8 Daten: Input
71        DDRB = (1 << DDB0); // Nur LED als Output, alles andere Input
72        DDRC = (1 << DDC6); // Nur Pick Command als Output, alles andere Input
73        DDRD = 0x00; // UART halt, IM jetzt auch
74
75        // Pin Change Intterupts aufsetzen
76        // PCMSK*: Pin Change Masks (welche Pins tragen zum Intterupt bei)
77        PCMSK1 = (1 << PCINT_ERROR) | (1 << PCINT_HCK) | (1 << PCINT_MOCK); // PORT B
78        PCMSK2 = (1 << PCINT_BSY) | (1 << PCINT_RDY);                     // PORT C
79        // Pin Change Interrupt Control Register: PC Interrupt Enable for Port B & C
80        PCICR = (1 << PCIE1) | (1 << PCIE2);
81
82        // Ausgezeichnete Interrupts aufsetzen (Index Mark IM haengt an INT0)
83        EICRA = (1 << ISC01) | (1 << ISC00); // rising edge von INT0 erzeugt intterupt
84        EIMSK = (1 << INT0); // interrupt fuer INT0 anschalten
85
86        // interrupt enable
87        sei();
88}
89
90
91void UartInit(uint32_t Baud)
92{
93        int BaudRate = OSCSPEED / (16 * Baud) - 1;
94
95        UBRR0H = (unsigned char) BaudRate>>8;
96        UBRR0L = (unsigned char) BaudRate;
97        //set BaudRate
98
99        UCSR0B = UCSR0B | (0b00011000);
100        // RXEN & TXEN enable (Bits 4, 3 = 1)
101
102        UCSR0C = (UCSR0C | (0b10000110));
103        // USART Register Select (Bit 7 = 1)
104        // 8 data bits per frame (Bit 2, 1 = 1)
105
106        UCSR0C = UCSR0C & 0b11110111;
107        // 1 Stop bit (Bit 3 = 0)     
108}
109
110
111
112
113int main()
114{
115        card_buffer_flush();
116        Init();
117        UartInit(38400);
118        start_led();
119
120        // nette sachen machen koennen
121        stdout = &uart;
122
123        user_input_loop();
124        return 0;
125}
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License