Advertisement
Guest User

creativiteit begint op te raken

a guest
Feb 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /*
  2. * opdracht2c.c
  3. *
  4. * Created: 17/02/2018 20:27:52
  5. * Author : Willem
  6. */
  7.  
  8. #define F_CPU 16000000
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11. #include <avr/interrupt.h>
  12. #include <stdlib.h>
  13.  
  14. char leesNummer();
  15. void binaryDisplay(int x);
  16. ISR(USART_RXC_vect);
  17. int teller = 0;
  18.  
  19. void writeChar(char x);
  20. void writeString(char st[]);
  21. void writeInt(int i);
  22.  
  23. int main(void) {
  24. sei();
  25.  
  26. UCSR0B = (1 << TXEN0); // Enable de USART Transmitter
  27. UCSR0C |= (1 << UCSZ01 | 1 << UPM01) ; /* 7 data bits, */
  28. UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
  29. UBRR0H=00;
  30. UBRR0L=103; //baudrade 9600 bij
  31. DDRB = 0b00011111;
  32.  
  33. while (1) {
  34. for(uint8_t i = 0; i <= 1; i++){
  35. if(i == 0){
  36. PORTB &= ~(1 << 4);
  37. _delay_ms(1000);
  38. writeInt(teller);
  39. }
  40. else if(i == 1){
  41. if(teller > 0){
  42. PORTB &= ~(1 << 3);
  43. PORTB &= ~(1 << 2);
  44. PORTB &= ~(1 << 1);
  45. PORTB &= ~(1 << 0);
  46. binaryDisplay(teller);
  47. teller = teller - 1;
  48. PORTB |= (1 << 4);
  49. i = -1;
  50. }
  51. _delay_ms(1000);
  52. }
  53. }
  54. PORTB &= ~(1 << 3);
  55. PORTB &= ~(1 << 2);
  56. PORTB &= ~(1 << 1);
  57. PORTB &= ~(1 << 0);
  58. }
  59. return (0);
  60. }
  61.  
  62. void binaryDisplay(int x){
  63. if(x >= 8){
  64. PORTB |= (1 << 3);
  65. x = x - 8;
  66. }
  67. if(x >= 4){
  68. PORTB |= (1 << 2);
  69. x = x - 4;
  70. }
  71. if(x >= 2){
  72. PORTB |= (1 << 1);
  73. x = x - 2;
  74. }
  75. if(x >= 1){
  76. PORTB |= (1 << 0);
  77. x = x - 1;
  78. }
  79. }
  80. char leesNummer() {
  81. while(!(UCSR0A & (1<<RXC0)));
  82. return UDR0;
  83. }
  84. int hexToInt(char c) {
  85. char hex[] = "0123456789ABCDEF"; // de HEX waarden
  86. int output = 0; // de output variable
  87.  
  88. for(int i = 0; i <= 15; i++) { // loop door de hex char array
  89. if(hex[i] == c) { output = i; } // als de hex waarde gelijk is aan de ingegeven waarde is dat de output
  90. }
  91.  
  92. return output; // return de output
  93. }
  94.  
  95. ISR(USART_RX_vect){
  96. char c = leesNummer();
  97. teller = hexToInt(c);
  98. }
  99.  
  100. void writeChar(char x) {
  101. while(~UCSR0A & (1 << UDRE0));
  102. UDR0 = x;
  103. }
  104.  
  105. void writeString(char st[]) {
  106. for(uint8_t i = 0 ; st[i] != 0 ; i++) {
  107. writeChar( st[i] );
  108. }
  109. }
  110.  
  111. void writeInt(int i) {
  112. char buffer[8];
  113. itoa(i,buffer,10);
  114. writeString(buffer);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement