编译环境:bascom avr+Proteus8.9
单片机源程序如下:
- rem Main.bas file generated by New Project wizard
- rem
- rem Created: 周二 6月 2 2020 TAOTIE
- rem Processor: ATmega8
- rem Compiler: BASCOM-AVR
- rem Write your code here
- $regfile = "m8def.dat"
- $crystal = 8000000
- '$baud = 9600
- $hwstack = 32
- $swstack = 30
- $framesize = 40
- Config Scl = Portc.5 ' 用I2C引脚Scl = Portc.5 ,Sda = Portc.4
- Config Sda = Portc.4
- Config Twi = 400000 ' i2c 的速度
- Ddrc.3 = 0 '配置端口方向输入(启动键)
- Portc.3 = 1 '上拉电阻有效
-
- I2cinit
- $lib "i2c_twi.lbx" ' 不使用模拟I2c的软件,而是使用twi
- $lib "glcdSSD1306-I2C.lib" ' 用glcdSSD1306-I2C库替换默认库要添加在bascom avr库中
- #if _build < 20784
- Dim ___lcdrow As Byte , ___lcdcol As Byte ' 老版本变量格式进行编译
- #endif
- Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306" '配置图形显示器
- DIM X AS bit '时间闸门标志
- dim jisu as word '脉冲计数变量
- 'Config Timer1 = Counter , Edge = Rising 'Timer1工作在计数方式上升沿有效
- Config Timer1 = Counter , Edge = Falling 'Timer1工作在计数方式下降沿有效
- Config Timer0 = Timer , Prescale = 8 'Timer0工作在计时方式预分频=8
- On Ovf0 Tim0_isr '计时器中断服务标号Tim0_isr
- tcnt0=&h06 '从6开始计250次 中断1次 250us
- Enable Timer0 '启用计时器中断
- Enable Interrupts
-
- do '外循环
- BITWAIT pinc.3, RESET '等待pinc.3置零启动运行
- jisu=0 '脉冲计数变量初始化
- x=0 '时间闸门标志初始化
- stop Timer1 '停止计数计时
- stop Timer0
- Tcnt1 = 0 ' 计时计数寄存器初始为零
- Timer1 =0
-
-
- start Timer1 '启动Timer1
- start Timer0 '启动Timer0
- cls '清屏
- Do '内循环
- Setfont font12x16
- Lcdat 2, 8 , "km/hr=" ;Tcnt1
- if x =1 then exit do '如果时间闸门标志=1则跳出内循环
-
-
- Loop
- loop '循环返回到外循环开始处
- end
- Tim0_isr:
- tcnt0=&h06 '进入中断先赋初值
- incr jisu '每次进入脉冲计数变量加1’
- if jisu=9286 then '如果脉冲计数变量等于时间闸门需求时间闸门标志=1
- x=1
- end if
- Return
- $include "../font12x16.font"
-
复制代码
所有资料51hei提供下载:
计数.zip
(38.26 KB, 下载次数: 44)
|