// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include "ee302lcd.h" // Include LCD header file
#include <stdio.h> // Include Standard I/O header file
#include "I2C_EE302.h"
#define GREEN_LED RC0 // Define RC0 as GREEN_LED
#define RED_LED RC1 // Define RC1 as RED_LED
#define KEY1 RB2 //label RB2 as key1
#define KEY2 RB3 //label RB3 as key2
unsigned char outString[5]; //character array for LCD string
unsigned char outString2[5]; //存放光照阈值 用于显示
int adc_value, volts, decivolts; //used to store the conversion of ADC value to volts and decivolts.
int light; //value of light threshold
int int_num;
char flag=0;
//串口发送
void transmit_string (char *p)
{
while (*p != '\0') //While string does not equal Null character
//do the following.
{
while (!TXIF); //Wait until TXREG empty.
TXREG = *p; //Load TXREG with character from string pointed to
p++; // by p, then increment p.
}
}
void do_adc(void)
{
__delay_us(30); //Acqusition time delay set to 30us (>19.7us)
GO_nDONE=1; //Start ADC conversion
while(GO_nDONE)continue; //Wait until conversion finishes
adc_value = ADRESH; //Pass ADRESH value to adc_value variable
sprintf(outString,"%d",ADRESH);
Lcd8_Clear(); //clear LCD
Lcd8_Write_String(outString);
}
//设置光照阈值 当测到的光照大于阈值时,亮灯 用两个按键设置+和-
void set_threshold(void)
{
if(KEY1==0)
{
__delay_ms(100);
if(KEY1==0)
light+=10;
}
if(KEY2==0)
{
__delay_ms(100);
if(KEY2==0)
light-=10;
}
}
//根据AD的值和阈值 判断要不要开灯 再将阈值显示到LCD 并将其存入到EEPROM中
void jugle(void)
{
if(adc_value>=light) //如果光照太强 那么关灯
GREEN_LED=0;
else //光照太弱 开灯
GREEN_LED=1;
sprintf(outString2,"%d",light);
Lcd8_Set_Cursor(2,0); // select line 2 of LCD
Lcd8_Write_String(outString2);
write_ext_eeprom(0x00, 0x01, light); //store the data to eeprom
transmit_string(outString); //将测到的光照强度通过串口打印出去
}
void main(void)
{
i2c_init(); //do i2c intialisation, TRISC modified here
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
TRISC=0x00; // Ensure LEDs are set as outputs RC0, RC1
TRISB=0x0C; // Set PORTB bit 2 3 as input for switch
PORTB=0X0C; //enable internal pull up
OPTION_REG=0X00; //turn on pull up in port B
//iic
TRISC |= 0x18; // RC6 and RC7 must be configured as inputs to enable the UART
// RC4 and RC3 high from I2C_init
TRISA=0x04; // Set PORTA bit 3 as input for AN2
ADCON0 = 0b01010001; // ADCS1:ADCS0 set to 0:1 for Tosc x8 (Fosc/8)
// Channel 2
// ADC On
ADCON1 = 0b00000010; // Left justified result
// ADCS2 = 0 for Tosc x8 (Fosc/8)
// RA2/AN2 selected as analog input
// Vref+ : Vdd Vref- : Vss
// AN7, AN6 and AN5 selected for Digital I/O for LCD
//USART
TRISC |= 0xC0; // RC6 and RC7 must be configured as inputs to enable the UART
TXSTA = 0x24; // Set TXEN and BRGH
RCSTA = 0x80; // Enable serial port by setting SPEN bit
SPBRG = 0x19; // Select 9600 baud rate.
//TIMER1
T1CON = 0x11; //enables timer1, sets it to internal clock, sets prescaler to x2
//(this was done to half frequency of LED's to correct value)
TMR1H = 0xEC; //set 60535 --->65535-60535=5000.
TMR1L = 0x77; //5000*2us = 10ms
INTCON = 0xC0; //enables global interrupts and peripherial interrupts
TMR1IE = 1; //enables timer1 interrupt
TMR1IF = 0; //clear Timer 1 interrupt flag
light=read_ext_eeprom(0x00, 0x01); // Read byte at address 0x0001 on EEPROM
while(1)
{
if(flag==1)
{
flag=0;
do_adc();
jugle();
}
set_threshold();
}
}
void __interrupt()
isr() { //interrupt service routine
if (TMR1IF == 1) //Check if Timer1 Interrupt?
{
TMR1IF=0; //resets timer1 interrupt flag
TMR1H = 0xEC; //Pre-load Timer1 so it is not counting from 0.
TMR1L = 0x77; //sets timer to 3035 -> 65535 - 3035 = 62500
//62500*2us = 0.125s) -> on/off = 4Hz
flag=1;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于PIC16F877A单片机的智能灯光
共39个文件
xml:5个
mk:4个
p1:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 5 下载量 74 浏览量
2023-12-18
13:28:37
上传
评论
收藏 217KB ZIP 举报
温馨提示
使用PIC16F877A单片机,检测环境关照,当光照比阈值低的时候,开灯。光照阈值可以通过按键进行设置,同时阈值可以保存在EEPROM中,断电不丢失。使用LCD1602进行显示,第一行显示测到的实时光照强度,第二行显示设置的光照阈值。 此外,测得的光照强度可以通过串口发送到上位机进行显示!! 文件里面给出了代码和protues仿真文件
资源推荐
资源详情
资源评论
收起资源包目录
smart_led.zip (39个子文件)
smart_led
Last Loaded 智能灯光.pdsbak 23KB
智能灯光.pdsprj 23KB
Backup Of 智能灯光.pdsbak 10KB
smart_led.X
ee302lcd.h 3KB
nbproject
Package-default.bash 1KB
configurations.xml 33KB
Makefile-default.mk 7KB
Makefile-variables.mk 429B
project.xml 1KB
Makefile-impl.mk 2KB
Makefile-local-default.mk 2KB
Makefile-genesis.properties 500B
private
configurations.xml 861B
private.xml 431B
Makefile 3KB
I2C_EE302.h 6KB
debug
default
dist
default
production
smart_led.X.production.cmf 44KB
smart_led.X.production.hex 7KB
__eeprom.i 40KB
smart_led.X.production.elf 18KB
memoryfile.xml 449B
smart_led.X.production.hxl 4KB
__eeprom.p1 22KB
doprnt.d 86B
smart_led.X.production.map 53KB
smart_led.X.production.rlf 267KB
__eeprom.d 87B
doprnt.p1 7KB
smart_led.X.production.o 33KB
smart_led.X.production.mum 465B
smart_led.X.production.sym 13KB
smart_led.X.production.sdb 9KB
smart_led.X.production.lst 198KB
doprnt.i 10KB
build
default
production
main.i 45KB
main.p1 38KB
main.p1.d 78B
main.c 5KB
智能灯光.pdsprj.USER-20190602CH.Administrator.workspace 4KB
共 39 条
- 1
资源评论
- qq_355065552024-01-04资源不错,对我启发很大,获得了新的灵感,受益匪浅。
- 捌捌捌捌捌捌捌捌2024-05-15资源不错,内容挺好的,有一定的使用价值,值得借鉴,感谢分享。
- m0_749686932024-01-04资源有一定的参考价值,与资源描述一致,很实用,能够借鉴的部分挺多的,值得下载。
- erhao0023562024-03-16资源使用价值高,内容详实,给了我很多新想法,感谢大佬分享~
- zaPoO1542024-03-11这个资源对我启发很大,受益匪浅,学到了很多,谢谢分享~
嵌入式小李
- 粉丝: 3w+
- 资源: 146
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功