Help Urgent -- please check my uC code.

Discussion in 'Embedded Systems and Microcontrollers' started by Sharik, Oct 12, 2015.

  1. Sharik

    Thread Starter New Member

    Oct 12, 2015
    1
    0
    can someone please chek my code i just cant figure it out with the display when i run the program the numbers doesnt show correctly on my display

    Code (C):
    1.  
    2. #include <avr/io.h>
    3. #include <util/delay.h>
    4. #include <stdlib.h>
    5. #include <avr/interrupt.h>
    6. #include <stdbool.h>
    7.  
    8. int mode;
    9. int counter = 0;
    10. int counters = 0;
    11. int freq = 0;
    12. int interruptcount = 0;
    13. int screen [4] = {11, 11, 11, 11}; // everything off
    14. int setpoint = 10;
    15. int direction = 0;
    16.  
    17. const unsigned char cijfers[] =
    18. {
    19. 0x82, // 1000 0010 = 0
    20. 0xBB, // 1001 1111 = 1
    21. 0x85, // 1000 0101 = 2
    22. 0x91, // 1001 0001 = 3
    23. 0xB8, // 1101 1000 = 4
    24. 0xD0, // 1101 0000 = 5
    25. 0xC0, // 1100 0000 = 6
    26. 0x9B, // 1001 1011 = 7
    27. 0x80, // 1000 0000 = 8
    28. 0x90, // 1000 1000 = 9
    29. 0x00, // 0000 0000 = Everything ON
    30. 0xFF // 1111 1111 = Everything OFF
    31. };
    32.  
    33. ISR(INT0_vect)
    34. {
    35. if(setpoint >= 90)
    36. {
    37. setpoint = 10;
    38. }
    39. else
    40. {
    41. setpoint += 10;
    42. }
    43. }
    44. ISR(TIMER0_OVF_vect)
    45. {
    46. TCNT0 = 133;//
    47. if (++interruptcount == 500)
    48. {
    49. if(counter == setpoint)
    50. {
    51. direction = 1;
    52. }
    53. else if(counter == 0)
    54. {
    55. direction = 0;
    56. }
    57. if (direction == 1)
    58. {
    59. counter--;
    60. }
    61. else
    62. {
    63. counter++;
    64. }
    65. interruptcount = 0;// reset counters
    66. counters = 0;
    67. }
    68. }
    69. int main(void)
    70. {
    71. TCCR0 = _BV(CS01);// de prescaling is 8
    72. TCNT0 = 135;// start timer with 135
    73. TIMSK = _BV(TOIE0);// interrupt timer0 overflow
    74. GICR = _BV(INT0);// enable externe interrupt bit INT0
    75. MCUCR = _BV(ISC01 || ISC00);// neergaande flank ISC01=1 ISC00=0
    76. DDRA = 0xFF; // display
    77. DDRB = 0xFF; // 7 segmeten display
    78. DDRD = 0x00; //
    79. int multiplexer = 0x01;
    80. sei();// zet interrupt flag
    81.  
    82. while(1)
    83. {
    84. setScreen(counter);// setscreen functie
    85.  
    86. //multiplexen of the  4 segmenten
    87. for(int i = 3; i >= 0; i--)
    88. {
    89. PORTA = 0x7F-(multiplexer);
    90. PORTB = cijfers[screen[I]];
    91. _delay_ms(2);
    92. multiplexer = multiplexer*2;
    93. }
    94. multiplexer = 0x01;
    95. PORTA = 0x77;
    96. PORTB = cijfers[setpoint/10];
    97. _delay_ms(1);
    98. PORTA = 0x7B;
    99. PORTB = cijfers[0];
    100. _delay_ms(1);
    101. }
    102. }
    103. void setScreen(int number)
    104. {
    105. if(number <= 9)
    106. {
    107. screen[0] = 11;
    108. screen[1] = 11;
    109. screen[2] = 11;
    110. screen[3] = number;
    111. }
    112. else if(number <= 99)
    113. {
    114. screen[0] = 11;
    115. screen[1] = 11;
    116. screen[2] = (number /10) %10;
    117. screen[3] = number % 10;
    118. }
    119. else if(number <= 999)
    120. {
    121. screen[0] = 11;
    122. screen[1] = (number / 100) %10;
    123. screen[2] = (number /10) %10;
    124. screen[3] = number % 10;
    125. }
    126. else
    127. {
    128. screen[0] = (number / 1000) % 10;
    129. screen[1] = (number / 100) % 10;
    130. screen[2] = (number / 10) % 10;
    131. screen[3] = number % 10;
    132. }
    133. }
    134.  
    Moderators note: used code tags
     
    Last edited by a moderator: Oct 13, 2015
  2. Andreas

    Active Member

    Jan 26, 2009
    68
    4
    Is that C++ with a bit of Assembly thrown in?
    Which AVR mC are you programming?

    I can't help you directly, but indirectly I can tell you that (for a start) you may want to be more descriptive with your subject line if you want other people to help you. A better lay out of your code and more comments can only help too. You may want to rethink and rewrite your main description and objectives. What you hope to achieve and the problems you are experiencing.

    Best of luck.
     
  3. ScottWang

    Moderator

    Aug 23, 2012
    4,850
    767
    You need to attach the circuit of uC to let the members to help you.
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,339
    1,022
    Assuming a typical hookup (schematic please):
    Recheck your logic at line 89 (PORTA = 0x7F-(multiplexer) It does not rotate the single bit digit select like you want.
    Also, both the segment outputs and digit outputs appear to be active low. Unless you are using external drivers, that won't work.
    As @ScottWang says, a schematic is necessary to do much more.
    What SPECIFICALLY is the display showing when it should SPECIFICALLY show something else?

    Edit: Welcome to AAC! Here you will find many people able and willing to help you if you provide the requested information.
     
    Last edited: Oct 14, 2015
Loading...