[PIC®/AVR®/dsPIC®产品] 去年我在Atmel官网下载的8051例子,分享

[复制链接]
 楼主| xixi2017 发表于 2023-8-16 15:23 | 显示全部楼层 |阅读模式
不知道从何时开始,Atmel官网没了,再也找不到那些相关的资源了。翻看了我的下载记录,找到了几个8051的官方例子,分享过来。
  1. #include <reg52.h>
  2. #include <stdio.h>

  3. /*------------------------------------------------
  4. Timer 2 Interrupt Service Routine.

  5. Set a breakpoint on 'overflow_count++' and run the
  6. program in the debugger.  You will see this line
  7. executes every 1000 clock cycles (or 1,000 Hz).

  8. So, overflow_count is actually a 1/1,000 sec
  9. timer.
  10. ------------------------------------------------*/
  11. static unsigned long overflow_count = 0;

  12. void timer1_ISR (void) interrupt 5
  13. {
  14. TF2 = 0;            /* Clear the interrupt request */
  15. overflow_count++;   /* Increment the overflow count */
  16. }

  17. /*------------------------------------------------
  18. MAIN C function
  19. ------------------------------------------------*/
  20. void main (void)
  21. {
  22. /*--------------------------------------
  23. Set Timer2 for 16-bit auto-reload.
  24. The timer counts to 0xFFFF, overflows,
  25. is reloaded, and generates an interrupt.
  26. --------------------------------------*/
  27. T2CON = 0x80;                /* 10000000 */

  28. /*--------------------------------------
  29. Set the reload values to be 1000 clocks.
  30. --------------------------------------*/
  31. RCAP2L = (65536UL-1000UL);
  32. RCAP2H = (65536UL-1000UL) >> 8;

  33. TL2 = RCAP2L;
  34. TH2 = RCAP2H;

  35. /*--------------------------------------
  36. --------------------------------------*/
  37. ET2 = 1;                      /* Enable Timer 2 Interrupts */
  38. TR2 = 1;                      /* Start Timer 2 Running */
  39. EA = 1;                       /* Global Interrupt Enable */

  40. /*--------------------------------------
  41. Do Nothing.  Actually, the timer 2
  42. interrupt will occur every 1000 clocks.
  43. Since the oscillator runs at 12 MHz,
  44. the interrupt will happen every 1 KHz.
  45. --------------------------------------*/
  46. while (1)
  47.   {
  48.   }
  49. }



 楼主| xixi2017 发表于 2023-8-16 15:24 | 显示全部楼层
  1. sfr P1 = 0x90;          /* SFR definition for Port 1 */
  2. sfr P3 = 0xB0;          /* SFR definition for Port 3 */

  3. /*------------------------------------------------
  4. MAIN C Function
  5. ------------------------------------------------*/
  6. void main (void)
  7. {
  8. unsigned char pval;     /* temp variable for port values */

  9. P1 = 0xFF;              /* Setup P1 for Input */

  10. /*--------------------------------------
  11. Use the Toolbox buttons in the debugger
  12. to change the value of P1.  Open the
  13. Port 1 and Port 3 dialogs from the
  14. Peripherals Menu to view their status.
  15. --------------------------------------*/
  16. while (1)
  17.   {
  18.   pval = P1;            /* Read P1 into pval */
  19.   P3 = pval;            /* Write pval to P3 */
  20.   }
  21. }

 楼主| xixi2017 发表于 2023-8-16 15:24 | 显示全部楼层
  1. #include <REG52.H>

  2. /*=============================================================================
  3. =============================================================================*/
  4. unsigned char ex0_isr_counter = 0;

  5. void ex0_isr (void) interrupt 0
  6. {
  7. ex0_isr_counter++;   // Increment the count
  8. }

  9. /*=============================================================================
  10. =============================================================================*/
  11. void main (void)
  12. {

  13. /*-----------------------------------------------
  14. Configure INT0 (external interrupt 0) to generate
  15. an interrupt on the falling-edge of /INT0 (P3.2).
  16. Enable the EX0 interrupt and then enable the
  17. global interrupt flag.
  18. -----------------------------------------------*/
  19. IT0 = 1;   // Configure interrupt 0 for falling edge on /INT0 (P3.2)
  20. EX0 = 1;   // Enable EX0 Interrupt
  21. EA = 1;    // Enable Global Interrupt Flag

  22. /*-----------------------------------------------
  23. Wait forever.
  24. -----------------------------------------------*/
  25. while (1)
  26.   {
  27.   }
  28. }

  29. /*=============================================================================
  30. =============================================================================*/

 楼主| xixi2017 发表于 2023-8-16 15:24 | 显示全部楼层
  1. /*-----------------------------------------------------------------------------
  2. Definitions for P1 (8 bits), P1.0, and P1.1.
  3. -----------------------------------------------------------------------------*/
  4. sfr P1 = 0x90;        /* SFR for P1 */

  5. sbit P1_0 = P1^0;     /* SFR for P1.0 */
  6. sbit P1_1 = P1^1;     /* SFR for P1.1 */

  7. /*-----------------------------------------------------------------------------
  8. MAIN C function
  9. -----------------------------------------------------------------------------*/
  10. void main (void)
  11. {
  12. P1_0 = 1;                  /* Configure P1.0 as an input */

  13. /*-----------------------------------------------
  14. This loop reads P1.0 and writes the value read
  15. to P1.1.

  16. Open the Port 1 Dialog from the Peripherals Menu
  17. and change the value of the P1.0 Pin.  When you
  18. set P1.0 LO, P1.1 outputs LO.  When you set
  19. P1.0 HI, P1.1 outputs HI.
  20. -----------------------------------------------*/
  21. while (1)
  22.   {
  23.   P1_1 = P1_0;            /* Copy P1.0 to P1.1 */
  24.   }
  25. }

  26. /*-----------------------------------------------------------------------------
  27. -----------------------------------------------------------------------------*/

 楼主| xixi2017 发表于 2023-8-16 15:25 | 显示全部楼层
如果谁有齐全的官方例子,可以分享过来。
duo点 发表于 2023-8-18 11:34 | 显示全部楼层
齐全的官方例子有点难搞啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

144

主题

2006

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

144

主题

2006

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部