打印
[PIC®/AVR®/dsPIC®产品]

去年我在Atmel官网下载的8051例子,分享

[复制链接]
826|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xixi2017|  楼主 | 2023-8-16 15:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
不知道从何时开始,Atmel官网没了,再也找不到那些相关的资源了。翻看了我的下载记录,找到了几个8051的官方例子,分享过来。
#include <reg52.h>
#include <stdio.h>

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

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

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

void timer1_ISR (void) interrupt 5
{
TF2 = 0;            /* Clear the interrupt request */
overflow_count++;   /* Increment the overflow count */
}

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set Timer2 for 16-bit auto-reload.
The timer counts to 0xFFFF, overflows,
is reloaded, and generates an interrupt.
--------------------------------------*/
T2CON = 0x80;                /* 10000000 */

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

TL2 = RCAP2L;
TH2 = RCAP2H;

/*--------------------------------------
--------------------------------------*/
ET2 = 1;                      /* Enable Timer 2 Interrupts */
TR2 = 1;                      /* Start Timer 2 Running */
EA = 1;                       /* Global Interrupt Enable */

/*--------------------------------------
Do Nothing.  Actually, the timer 2
interrupt will occur every 1000 clocks.
Since the oscillator runs at 12 MHz,
the interrupt will happen every 1 KHz.
--------------------------------------*/
while (1)
  {
  }
}



使用特权

评论回复
沙发
xixi2017|  楼主 | 2023-8-16 15:24 | 只看该作者
sfr P1 = 0x90;          /* SFR definition for Port 1 */
sfr P3 = 0xB0;          /* SFR definition for Port 3 */

/*------------------------------------------------
MAIN C Function
------------------------------------------------*/
void main (void)
{
unsigned char pval;     /* temp variable for port values */

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

/*--------------------------------------
Use the Toolbox buttons in the debugger
to change the value of P1.  Open the
Port 1 and Port 3 dialogs from the
Peripherals Menu to view their status.
--------------------------------------*/
while (1)
  {
  pval = P1;            /* Read P1 into pval */
  P3 = pval;            /* Write pval to P3 */
  }
}

使用特权

评论回复
板凳
xixi2017|  楼主 | 2023-8-16 15:24 | 只看该作者
#include <REG52.H>

/*=============================================================================
=============================================================================*/
unsigned char ex0_isr_counter = 0;

void ex0_isr (void) interrupt 0
{
ex0_isr_counter++;   // Increment the count
}

/*=============================================================================
=============================================================================*/
void main (void)
{

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

/*-----------------------------------------------
Wait forever.
-----------------------------------------------*/
while (1)
  {
  }
}

/*=============================================================================
=============================================================================*/

使用特权

评论回复
地板
xixi2017|  楼主 | 2023-8-16 15:24 | 只看该作者
/*-----------------------------------------------------------------------------
Definitions for P1 (8 bits), P1.0, and P1.1.
-----------------------------------------------------------------------------*/
sfr P1 = 0x90;        /* SFR for P1 */

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

/*-----------------------------------------------------------------------------
MAIN C function
-----------------------------------------------------------------------------*/
void main (void)
{
P1_0 = 1;                  /* Configure P1.0 as an input */

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

Open the Port 1 Dialog from the Peripherals Menu
and change the value of the P1.0 Pin.  When you
set P1.0 LO, P1.1 outputs LO.  When you set
P1.0 HI, P1.1 outputs HI.
-----------------------------------------------*/
while (1)
  {
  P1_1 = P1_0;            /* Copy P1.0 to P1.1 */
  }
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/

使用特权

评论回复
5
xixi2017|  楼主 | 2023-8-16 15:25 | 只看该作者
如果谁有齐全的官方例子,可以分享过来。

使用特权

评论回复
6
duo点| | 2023-8-18 11:34 | 只看该作者
齐全的官方例子有点难搞啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

1791

帖子

1

粉丝