单片机流水灯代码

单片机流水灯代码,第1张

具体如下。
#include,sbitLED1=P1^0;//定义0/1变量,是因为在电路中一般为高电压以及低电压两种,也就是代表者0或1。sbitLED2=P1^1;sbitLED3=P1^2;sbitLED4=P1^3;sbitLED5=P1^4;sbitLED6=P1^5;sbitLED7=P1^6。
单片机(Microcontrollers)又称微控制器,由中央处理器、存储器、输入输出端口(包括并行I/O、串行I/O、模数转换器)、计时器和计数器等组成,具有完整数字处理功能的大规模集成电路。微控制器是一种面向控制领域嵌入式应用的集成化计算机芯片,主要用于工业控制、数据处理、信号处理、智能仪器、通信产品及民用消费产品等自动控制产品与器件中。通常也把它简称为MCU或μC,MCU配以适当的外围设备和软件就可构成一个计算机应用系统,所以也称之为单片微型计算机,简称为单片机

#include //52系列单片机头文件
#include //_crol_ 字符循环左移
#define uint unsigned int //宏定义
#define uchar unsigned char//宏定义
void delayms(uint); //声明子函数
uchar a; //定义uchar类型的变量a
void main() //主函数
{
a=0xfe; //赋初值11111110
while(1) //大循环
{
P1=a;
delayms(500); //延时500毫秒
a=_crol_(a,1); //将a循环左移1位后再赋给a
}
}
void delayms(uint xms) //延时函数
{
uint i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
学习单片机,还需要结合实际硬件电路来编程

ORG 0000H
LJMP MAIN
ORG 0030H
MAIN:
MOV 30H,#0FEH
MOV 31H,01H
MOV 32H,#55H
LOOP:
MOV A,P1
ANL A,#03H
JNZ LOOP1
MOV A,30H
MOV P0,A
RL A
MOV 30H,A
LCALL DELAY
SJMP LOOP
LOOP1:
DEC A
JNZ LOOP2
MOV A,30H
MOV P0,A
RR A
MOV 30H,A
LCALL DELAY
SJMP LOOP
LOOP2:
DEC A
JNZ LOOP3
MOV A,31H
MOV P0,A
CJNE A,#0FFH,LOOP21
MOV A,#01H
SJMP LOOP22
LOOP21:
SETB C
RLC A
LOOP22:
MOV 31H,A
LCALL DELAY
SJMP LOOP
LOOP3:
MOV A,32H
MOV P0,A
CPL A
MOV 32H,A
LCALL DELAY
LJMP LOOP
END

/
程序名称:hc595c
程序功能:595的应用文件
程序作者:吴鉴鹰
创建时间:2014-3-10
修改时间:
程序版本:V01
/
/
包含头文件
/
#include "inc/hc595h"
#include "reg51h"
/
定义引脚
/
/ 时钟信号线引脚定义 /
sbit HC595CLK = P0^5;
/ 片选信号线引脚定义 /
sbit HC595RCK = P0^6;
/ 数据输入引脚定义 /
sbit HC595DATA = P0^7;
/
函数名称:SendData
函数功能:74HC595数据的发送
入口参数:unsigned int uiDataOne, unsigned int uiDataTwo
出口参数:void
/
void SendData(unsigned int uiDataOne, unsigned int uiDataTwo)
{
unsigned int i = 0;
/ 将片选信号置为低电平 /
HC595RCK = 0;

/ 输入第一个数据:uiDataOne /
for (i = 0; i < 8; i++)
{
/ 给出脉冲信号,首先将CLK置为0 /
HC595CLK = 0;
if (0 != (uiDataOne & 0x80))
{
HC595DATA = 1;
}
else
{
HC595DATA = 0;
}
/ 给出脉冲信号,首先将CLK置为1 /
HC595CLK = 1;
/ 准备第二个数据 /
uiDataOne = uiDataOne << 1;
}

/ 输入第二个数据:uiDataTwo /
for (i = 0; i < 8; i++)
{
/ 给出脉冲信号,首先将CLK置为0 /
HC595CLK = 0;
if (0 != (uiDataTwo & 0x80))
{
HC595DATA = 1;
}
else
{
HC595DATA = 0;
}
/ 给出脉冲信号,首先将CLK置为1 /
HC595CLK = 1;
/ 准备第二个数据 /
uiDataTwo = uiDataTwo << 1;
}

/ 将片选信号置为高电平 /
HC595RCK = 1;
}

你从microchip官网上下一些PIC32的例程就行啦,各种外设接口的例程都有,下面是定时中断的一个例程。在Debug模式下能正常进中断,RD0接口的LED闪烁表示芯片是好的
#include <plibh>
// Configuration Bit settings
// SYSCLK = 72 MHz (8MHz Crystal/ FPLLIDIV FPLLMUL / FPLLODIV)
// PBCLK = 36 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
//
#pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
// Let compile time pre-processor calculate the PR1 (period)
#define FOSC 72E6
#define PB_DIV 8
#define PRESCALE 256
#define TOGGLES_PER_SEC 1
#define T1_TICK (FOSC/PB_DIV/PRESCALE/TOGGLES_PER_SEC)
int main(void)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//STEP 1 Configure cache, wait states and peripheral bus clock
// Configure the device for maximum performance
// This macro sets flash wait states, PBCLK divider and DRM wait states based on the specified
// clock frequency It also turns on the cache mode if avaialble
// Based on the current frequency, the PBCLK divider will be set at 1:2 This knoweldge
// is required to correctly set UART baud rate, timer reload value and other time sensitive
// setting
SYSTEMConfigPerformance(FOSC);
// Override PBDIV to 1:8 for this timer example
mOSCSetPBDIV(OSC_PB_DIV_8);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 2 configure Timer 1 using internal clock, 1:256 prescale
OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
// set up the timer interrupt with a priority of 2
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
// enable multi-vector interrupts
INTEnableSystemMultiVectoredInt();
// configure PORTDRD0 = output
mPORTDSetPinsDigitalOut(BIT_0);
while(1);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STEP 3 configure the Timer 1 interrupt handler
void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)
{
// clear the interrupt flag
mT1ClearIntFlag();
// things to do
// in this case, toggle the LED
mPORTDToggleBits(BIT_0);
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/dianzi/13359573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-21
下一篇 2023-07-21

发表评论

登录后才能评论

评论列表(0条)

保存