Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2013
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /*
  2.  * UsbControllerActivity.java
  3.  * This file is part of UsbController
  4.  *
  5.  * Copyright (C) 2012 - Manuel Di Cerbo
  6.  *
  7.  * UsbController is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * UsbController is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with UsbController. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20.  
  21. /*
  22.  * main.c
  23.  *  (c) Nexus-Computing GmbH Switzerland
  24.  *  Created on: Feb 02, 2012
  25.  *      Author: Manuel Di Cerbo
  26.  */
  27. #include <stdio.h>
  28. #include <avr/io.h>
  29. #include <avr/interrupt.h>
  30. #include <util/delay.h>
  31.  
  32. #define LED PB5 // LED is on Pin 13 or Pin 5 of Port B
  33. /*
  34.  * UART-Initialization from www.mikrocontroller.net
  35.  * Hint: They are awesome! :-)
  36.  */
  37.  
  38. #ifndef F_CPU
  39. #warning "F_CPU was not defined, defining it now as 16000000"
  40. #define F_CPU 16000000UL
  41. #endif
  42.  
  43. #define BAUD 9600UL      // baud rate
  44. // Calculations
  45. #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // smart rounding
  46. #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // real baud rate
  47. #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // error in parts per mill, 1000 = no error
  48. #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
  49. #error Error in baud rate greater than 1%!
  50. #endif
  51.  
  52. void uart_init(void) {
  53.   UBRR0H = UBRR_VAL >> 8;
  54.   UBRR0L = UBRR_VAL & 0xFF;
  55.  
  56.   UCSR0C = (0 << UMSEL01) | (0 << UMSEL00) | (1 << UCSZ01) | (1 << UCSZ00); // asynchron 8N1
  57.   UCSR0B |= (1 << RXEN0); // enable UART RX
  58.   UCSR0B |= (1 << TXEN0); // enable UART TX
  59.   UCSR0B |= (1 << RXCIE0); //interrupt enable
  60. }
  61.  
  62. /* Receive symbol, not necessary for this example, using interrupt instead*/
  63. uint8_t uart_getc(void) {
  64.   while (!(UCSR0A & (1 << RXC0)))
  65.     // wait until symbol is ready
  66.     ;
  67.   return UDR0; // return symbol
  68. }
  69.  
  70. uint8_t uart_putc(unsigned char data) {
  71.   /* Wait for empty transmit buffer */
  72.   while (!(UCSR0A & (1 << UDRE0)))
  73.     ;
  74.   /* Put data into buffer, sends the data */
  75.   UDR0 = data;
  76.   return 0;
  77. }
  78.  
  79.  
  80. void initIO(void) {
  81.   DDRD |= (1 << DDD3);
  82.   DDRB = 0xff; //all out
  83. }
  84.  
  85.  
  86. volatile uint8_t data = 10;
  87.  
  88. int main(void) {
  89.   initIO();
  90.   uart_init();
  91.   sei();
  92.  
  93.   uint8_t i = 0;
  94.   volatile uint8_t pause;
  95.   for(;;) {
  96.     pause = data;
  97.     if(pause == 1) {
  98.       digitalWrite(13, HIGH);
  99.     } //end of if
  100.  
  101.     else if (pause == 0) {
  102.       digitalWrite(13, LOW);
  103.     } //end of else if
  104.  
  105.   }
  106.   return 0; // never reached
  107. }
  108.  
  109. ISR(USART_RX_vect) {//attention to the name and argument here, won't work otherwise
  110.   data = UDR0;//UDR0 needs to be read
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement