• 【无标题】


    1.set output compare register 1B

    In assembly language

    1. ldi r16,high(4321);
    2. sts OCR1BH,r16;
    3. ldi r16,low(4321);
    4. sts OCR1BL,r16;

    In C

    OCR1B=4321;

    2. To toggle OC1A pin (what pin is this?) 8 times per second (i.e. 4Hz period)

    1. #include <avr/io.h>
    2. /*
    3. * main -- Main program
    4. */
    5. int main(void)
    6. {
    7. /* Set OC1A pin to be an output */
    8. /* Pin OC1A is Port D, pin 5 */
    9. DDRD = (1<<5);
    10. /* We want to count 1,000,000 clock cycles between each toggle of the pin.
    11. ** We choose to do this with a prescale of /64 and to count 15625 cycles,
    12. ** i.e. we count from 0 to 15624. */
    13. /* Set output compare register value */
    14. OCR1A = 15624;
    15. /* Set timer counter control registers A and B so that
    16. * - mode is - clear counter on compare match (WGM bits are 0100)
    17. * - output compare match action is to toggle pin OC1A (COM1A bits are 01)
    18. * - correct clock prescale value is chosen.
    19. * TCCR1C can just stay as default value (0).
    20. */
    21. TCCR1A = (0 << COM1A1) | (1 << COM1A0) // Toggle OC1A on compare match
    22. | (0 << WGM11) | (0 << WGM10); // Least two significant WGM bits
    23. TCCR1B = (0 << WGM13) | (1 << WGM12) // Two most significant WGM bits
    24. | (0 << CS12) | (1 << CS11) | (1 <<CS10); // Divide clock by 64
    25. /* Do nothing forever - the hardware takes care of everything */
    26. while(1) {
    27. ;
    28. }
    29. }

    5.7 segment display connected to port A, with CC (digit select) connected to port D, pin 0

    1. #include <avr/io.h>
    2. uint8_t seven_seg[10] = { 63,6,91,79,102,109,125,7,127,111};
    3. /* Display digit function. Arguments are the digit number (0 to 9)
    4. * and the digit to display it on (0 = right, 1 = left). The function
    5. * outputs the correct seven segment display value to port A and the
    6. * correct digit select value to port D, pin 0.
    7. * See Lecture 14 example code for some code to base this on.
    8. */
    9. void display_digit(uint8_t number, uint8_t digit)
    10. {
    11. PORTD = digit;
    12. PORTA = seven_seg[number]; // We assume digit is in range 0 to 9
    13. }
    14. /*
    15. * main -- Main program
    16. */
    17. int main(void)
    18. {
    19. uint8_t digit; /* 0 = right, 1 = left */
    20. uint8_t value;
    21. /* Set port A (all pins) to be outputs */
    22. DDRA = 0xFF;
    23. /* Set port D, pin 0 to be an output */
    24. DDRD = 1;
    25. /* Set up timer/counter 0 to count the number of rising
    26. ** edges on pin T0.
    27. */
    28. TCCR0A = 0;
    29. TCCR0B = (1<<CS02) | (1<<CS01) | (1<<CS00);
    30. /* Set up timer/counter 1 so that it reaches an output compare
    31. ** match every 1 millisecond (1000 times per second) and then
    32. ** resets to 0.
    33. ** We divide the clock by 8 and count 1000 cycles (0 to 999)
    34. */
    35. OCR1A = 999;
    36. TCCR1A = (0 << COM1A1) | (1 << COM1A0) // Toggle OC1A on compare match
    37. | (0 << WGM11) | (0 << WGM10); // Least two significant WGM bits
    38. TCCR1B = (0 << WGM13) | (1 << WGM12) // Two most significant WGM bits
    39. | (0 << CS12) | (1 << CS11) | (0 <<CS10); // Divide clock by 8
    40. /* Repeatedly output the digits. We keep track of which
    41. ** digit. 0 = right (ones place), 1 = left (tens place)
    42. */
    43. digit = 0;
    44. while(1) {
    45. /* Output the current digit */
    46. if(digit == 0) {
    47. /* Extract the ones place from the timer counter 0 value */
    48. /* HINT: Consider the modulus (%) operator. */
    49. value = TCNT0 % 10;
    50. } else {
    51. /* Extract the tens place from the timer counter 0 */
    52. value = (TCNT0 / 10) % 10;
    53. }
    54. display_digit(value, digit);
    55. /* Change the digit flag for next time. if 0 becomes 1, if 1 becomes 0. */
    56. digit = 1 - digit;
    57. /* Wait for timer 1 to reach output compare A value.
    58. * We can monitor the OCF1A bit in the TIFR1 register. When
    59. * it becomes 1, we know that the output compare value has
    60. * been reached. We can write a 1 to this bit to clear it.
    61. * See page 143-144 of datasheet for details.
    62. */
    63. while ((TIFR1 & (1 << OCF1A)) == 0) {
    64. ; /* Do nothing - wait for the bit to be set */
    65. }
    66. /* Clear the output compare flag - by writing a 1 to it. */
    67. TIFR1 &= (1 << OCF1A);
    68. }
    69. }
  • 相关阅读:
    遮挡Windows电脑上烦人的微信/企业微信/钉钉消息闪烁提醒
    【RocketMQ集群】Linux搭建RocketMQ双主双从集群
    品牌创意二维码营销活动:MoneyLion 在纽约全城“撒钱”,月增百万级曝光!
    算法思想 - 贪心算法
    theos tweak导入自定义类
    Spring Boot中实现发送文本、带附件和HTML邮件
    四种常用的自动化测试框架
    遨博机械臂URDF功能包ROS仿真
    ObjectBox 初探
    【硅谷创业公司招聘】openai 亚马逊投资的创业公司招聘远程全栈工程师
  • 原文地址:https://blog.csdn.net/weixin_61023150/article/details/133484441