Beruflich Dokumente
Kultur Dokumente
html
http://pt.scribd.com/doc/199324519/Cortex-m-Series
http://www.mikroe.com/forum/viewtopic.php?t=17449
https://www.youtube.com/watch?v=v1dLdfa-7Yw
http://saeedsolutions.blogspot.com.br/2012/10/pic16f84a-pwm-code-proteus-simulat
ion.html
// Constants for calculating temperature and humidity
// SHT1x / SHT7x V4 humidity conversion coefficients (12 bits)
const unsigned int C1 = 205;
// -2.0468
const unsigned int C2 = 367;
// 0.0367 (367 * 10^-4)
const unsigned short C3 = 16;
// -1.5955* 10^-6 (15.955 * 10^-7)
//SHT1x / SHT7x V4 temperature compensation coefficients (12 bits)
const unsigned int T1 = 1000;
// 0.01 (1*10^-2)
const unsigned int T2 = 8;
// 0.00008 (8 * 10^-5)
//SHT1x V4 temperature conversion coefficients (14 bits)
const unsigned int D1 = 4010;
// -40.1
const unsigned short D2 = 1;
// 0.01
unsigned short i, j, Select;
long int temp, k, SOt, SOrh, Tmp_res, Rh_res;
char *Tmp = "000.0 ";
char *Rh = "00.0 ";
void SHT_Reset() {
if(Select == 1) {
SCL_SCK = 0;
SDA_SDI = 1;
for (i = 1; i <= 10; i++)
SCL_SCK = ~SCL_SCK;
}
}
//
//
//
//
SCL low
define SDA as input
repeat 10 times
invert SCL
void Transmission_Start()
if(Select == 1) {
SDA_SDI_Direction
SCL_SCK = 1;
Delay_1us();
SDA_SDI_Direction
SDA_SDI = 0;
Delay_1us();
SCL_SCK = 0;
Delay_1us();
SCL_SCK = 1;
Delay_1us();
SDA_SDI_Direction
Delay_1us();
SCL_SCK = 0;
}
}
//
//
//
//
//
//
//
//
//
//
//
//
//
// MCU ACK
void MCU_ACK() {
if(Select == 1) {
{
= 1;
= 0;
= 1;
SDA_SDI_Direction = 0;
SDA_SDI = 0;
SCL_SCK = 1;
Delay_1us();
SCL_SCK = 0;
Delay_1us();
SDA_SDI_Direction = 1;
//
//
//
//
//
//
//
}
}
// This function returns temperature or humidity, depends on command
long int Measure(short command) {
j = command;
// j = command (0x03 or 0x05)
SHT_Reset();
// procedure for reseting SHT11
Transmission_Start();
// procedure for sTmprting transmission
k = 0;
// k = 0
if(Select == 1) {
SDA_SDI_Direction = 0;
// define SDA as output
SCL_SCK = 0;
// SCL low
for(i = 1; i <= 8; i++) {
// repeat 8 times
if (j.F7 == 1)
// if bit 7 = 1
SDA_SDI_Direction = 1;// define SDA as input
else {
// else (if bit 7 = 0)
SDA_SDI_Direction = 0;// define SDA as output
SDA_SDI = 0;
// SDA low
}
Delay_1us();
// 1us delay
SCL_SCK = 1;
// SCL high
Delay_1us();
// 1us delay
SCL_SCK = 0;
// SCL low
j <<= 1;
// move contents of j one place left
}
SDA_SDI_Direction = 1;
SCL_SCK = 1;
Delay_1us();
SCL_SCK = 0;
Delay_1us();
while (SDA_SDI == 1)
Delay_1us();
//
//
//
//
//
//
//
//
//
//
//
repeat 16 times
move contents of k one place left
SCL high
if SDA is high
}
return k;
// if counter i = 8 then
// MCU acknowledge
// returns contents of k
}
void STartUpDelay() {
Delay_ms(20);
}
void DisplayMeasurements(unsigned short rownumber) {
// Calculating temperature Tmp_res = D1 + D2 * SOt
// if temperature is positive
// calculate temperature
// else (if temperature is negative)
// calculate temperature
Tmp[5] = 12345 /
Tmp[6] = 12345 %
Tmp[7] = 12345 %
Tmp[9] = 12345 %
== '0')
= ' ';
= ' ';
// display temperatur
// display humidity o
#include <Io.h>
short div_hz;
int count, dt_tmr1, res_tmr1 = 65535;
short duty_cycle;
void interrupt() {
Soft_PWM_Inter(&PORTD, pin0);
/*if(TMR1IF_bit) {
if(!RD5_bit) {
TMR1L = duty_cycle;
RD5_bit = 1;
}
else if(RD5_bit) {
TMR1L = duty_cycle;
RD5_bit = 0;
}
TMR1IF_bit = 0;
TMR1H
= 0xFE;
TMR1L
= 0x0A;
}*/
}
/*//5000 Hz
void InitTimer0() {
T1CON = 0x01;
TMR1IE_bit = 1;
TMR1IF_bit = 0;
TMR1H = 0xFE;
TMR1L = 0x0A;
}*/
/*// 100 ms
void InitTimer0(){
T1CON = 0x31;
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
TMR1IE_bit
= 1;
}*/
unsigned int ADCBITS = 0b0000000000000000, ADCBITS_PERCENT, PWMBITS, i = 0;
char ADCSTRG[5], ADCSTRG_PERCENT[4], PWMSTR[4];
unsigned const long Hz = 2000;
/***************DISPLAYS THE ADC VALUE********************/
void LCD_ADC() {
//Leitura Analoga AN0
ADCBITS = ADC_Read(0);
ADCSTRG[0] = ((ADCBITS / 1000) + 48);
//milhar
ADCSTRG[1] = (((ADCBITS / 100) % 10) + 48); //centena
ADCSTRG[2] = (((ADCBITS / 10) % 10) + 48); //dezena
ADCSTRG[3] = ((ADCBITS % 10) + 48);
//unidade
Lcd_Out(1, 1, "AN0:");
Lcd_Chr(1, 6, ADCSTRG[0]);
Lcd_Chr(1, 7, ADCSTRG[1]);
Lcd_Chr(1, 8, ADCSTRG[2]);
Lcd_Chr(1, 9, ADCSTRG[3]);
//Conversao Analoga An0 para %
ADCBITS_PERCENT = (ADCBITS / 1.023);
ADCSTRG_PERCENT[1] = (((ADCBITS_PERCENT / 100) % 10) + 48); //centena
ADCSTRG_PERCENT[2] = (((ADCBITS_PERCENT / 10) % 10) + 48); //dezena
ADCSTRG_PERCENT[3] = ((ADCBITS_PERCENT % 10) + 48);
//unidade
Lcd_Chr(1, 11, ADCSTRG_PERCENT[0]);
Lcd_Chr(1, 12, ADCSTRG_PERCENT[1]);
Lcd_Chr(1, 13, ADCSTRG_PERCENT[2]);
Lcd_Chr_CP('.');
Lcd_Chr(1, 15, ADCSTRG_PERCENT[3]);
Lcd_Out(1, 16, "%");
PWMBITS = ADCBITS;
PWMSTR[0] = ((PWMBITS / 1000)
PWMSTR[1] = (((PWMBITS / 100)
PWMSTR[2] = (((PWMBITS / 10)
PWMSTR[3] = ((PWMBITS % 10)
Lcd_Out(2, 1, "CCP1:");
Lcd_Chr(2, 7, PWMSTR[0]);
Lcd_Chr(2, 8, PWMSTR[1]);
Lcd_Chr(2, 9, PWMSTR[2]);
Lcd_Chr(2, 10, PWMSTR[3]);
+
%
%
+
48);
//milhar
10) + 48); //centena
10) + 48); //dezena
48);
//unidade
T2CON = 0b00000101;
// /T2OUTPS3/T2OUTPS2/T2OUTPS1/T2OUTPS0/TMR2ON/T2
CKPS1/T2CKPS0 // 0bxxxxxx10 = 16 / 0bxxxxxx01 = 4
PR2
= 1023;
//PR2 = 1023 (Peridode 819,20 uS)
//PWM 1
CCP1IE_bit = 1;
CCP1CON= 0b10111100;
P1M0
CCPR1L = PWMBITS >> 2;
CCP1CON.B4 = PWMBITS;
CCP1CON.B5 = PWMBITS >> 1;
//PWM 2
CCP2IE_bit
CCP2CON =
CCPR2L =
CCP2CON.B4
CCP2CON.B5
= 1;
0b00111100;
PWMBITS >> 2;
= PWMBITS;
= PWMBITS >> 1;
//P1M1/P1M0/DC1B1/DC1B0/CCP1M3/CCP1M2/CCP1M1/CC
//Duty Cicle 10 bits
// / /DC2B1/DC2B0/CCP2M3/CCP2M2/CCP2M1/CCP2M0
//Duty Cicle 10 bits
//PWM 3
}
unsigned long error = 0;
char duty_change = 0;
void main() {
INTCON= 0b00000000; //Desarma uC
TRISA = 0b00000001;
ADCON0= 0b00000001; // / /CHS3/CHS2/CHS1/CHS0/GO-DONE/ADON
ADCON1= 0b00001110; // / /VCFG1/VCFG0/PCFG3/PCFG2/PCFG1/PCFG0
TRISB = 0b00000000;
TRISC = 0b00000000;
TRISD = 0b00000000;
INTCON= 0b11000000; //Arma uC
Reset_IO();
Lcd_Config();
//InitTimer0();
Soft_PWM_Init(100);
Soft_PWM_Start();
while(1) {
LCD_ADC();
duty_cycle = (100 * PWMBITS) / 1023;
Soft_PWM_set_duty(duty_cycle);
}
}
short i;
char chr_valor[4];
int valor = 1234;
chr_valor[0]
chr_valor[1]
chr_valor[2]
chr_valor[3]
=
=
=
=
(valor / 1000)
((valor / 100)
((valor / 10)
( valor % 10)
+ 48;
% 10) + 48;
% 10) + 48;
+ 48;
unsigned char b, i;
void I2CWait(){
while ((SSPCON2 & 0x1F) || (SSPSTAT & 0x04));
pending transfer */
}
void I2CStart(){
I2CWait();
SEN_bit = 1;
while(SEN_bit);
}
void I2CStop(){
PEN_bit = 1;
while(PEN_bit);
ically cleared by hardware
}
void I2CRestart(){
RSEN_bit = 1;
while(RSEN_bit);
}
void I2CAck(){
ACKDT_bit = 0;
ACKEN_bit = 1;
while(ACKEN_bit);
}
void I2CNak(){
ACKDT_bit = 1;
ACKEN_bit = 1;
while(ACKEN_bit);
}
#define
#define
#define
#define
SCL_SCK RC3
SDA_SDI RC4
HIGH_SPEED 400000
NORNAL_SPEED 100000
//400kHz
//100kHz
400kBps
100kBps
void I2CWait() {
while ((SSPCON2 & 0x1F) || (SSPSTAT & 0x04)); /* wait for any pending transf
er */
}
void I2CStart() {
I2CWait();
SSPCON2.SEN = 1;
while(SSPCON2.SEN);
}
void I2CRestart() {
I2CWait();
SSPCON2.RSEN = 1;
while(SSPCON2.RSEN);
}
void I2C_W(char address) {
I2CWait();
SSPBUF = address;
while(SSPSTAT.BF);
I2CWait();
if(!SSPCON2.ACKSTAT) PORTA.F0 =~ PORTA.F0;
}
unsigned char I2CReceive() {
I2CWait();
SSPCON2.RCEN = 1;
while(!SSPSTAT.BF);
return SSPBUF;
}
void I2CACK() {
I2CWait();
SSPCON2.ACKEN = 1;
while(SSPCON2.ACKEN);
}
void I2CStop() {
I2CWait();
SSPCON2.PEN = 1;
while(SSPCON2.PEN);
}
void I2C_Config() {
SSPCON1 = 0b00101000;
//WCOL/SSPOV/SSPEN/CKP/SSPM3/SSPM2/SSPM1/SSPM0
SSPSTAT = 0b01000000;
//SMP/CKE/DA/P/S/RW/UA/BF
I2C1_Init(NORNAL_SPEED);
SSPADD = 49;
//Clock = 100KHz SSPCON = 0b00101000; I2C Mast
er mode, clock = FOSC / (4 * (SSPADD+1))
}
/*
//24C02B
delay_ms(250);
I2CStart();
delay_us(4);
I2CSend(0xA0);
I2CSend(0x10);
I2CSend(0x25);
I2CStop();
delay_us(4);
I2CStart();
delay_us(4);
I2CSend(0xA0);
I2CSend(0x10);
I2CRestart();
delay_us(4);
I2CSend(0xA0+1);
PORTB=I2CReceive();
I2CStop();
delay_us(4);
*/
/*
// TC74
delay_ms(250);
I2CStart();
delay_us(4);
I2C1_Wr(0x9A);
I2C1_Wr(0x00);
I2C1_Repeated_Start();
delay_us(4);
I2C1_Wr(0x9B);
PORTB = I2C1_Rd();
I2C1_Stop();
delay_us(4);
*/
/*
// MCP3221
delay_ms(250);
I2CStart();
delay_us(4);
I2CSend(0x90+1);
PORTB=I2CReceive();
I2CACK();
PORTD=I2CReceive();
I2CStop();
*/
/*
// DS1621
delay_ms(250);
I2CStart();
I2CSend(0x92);
I2CSend(0xEE);
I2CRestart();
I2CSend(0x92);
I2CSend(0xAA);
I2CRestart();
I2CSend(0x92+1);
PORTB=I2CReceive();
I2CACK();
PORTD=I2CReceive();
I2CStop();
*/
unsigned short sec = 0;
/*
void interrupt() {
//Interrupcao de EEPROM
if(EEIF_bit == 1) {
RD3_bit = 1;
EEIF_bit = 0;
}
}
void ISR() {
//Interrupcao de TMR0
if(TMR1IE_bit == 1 && TMR1IF_bit == 1) {
sec++;
Lcd_Out(3,1,"3");
if(sec == 2) {
RD3_bit =~ RD3_bit;
sec = 0;
Lcd_Out(4,1,"4");
}
TMR1IF_bit = 0;
TMR0H = 0X0B;
TMR0L = 0XDC;
}
}
void Timer1_Config() {
T1CON = 0b10111000;
//RD16/T1RUN/T1CKPS1/T1CKPS0/T1OSCEN/T1SYNC/TMR1CS/TMR1O
N
TMR1H = 0X0B;
TMR1L = 0XDC;
TMR1IF_bit = 0;
TMR1ON_bit = 1;
Lcd_Out(2,1,"2");
TMR1ON_bit = 1;
}
*/
//IPR2 = 0b00010000;
//PIE1 = 0b00000001;
//IPR1 = 0b00000000;
//PIR1 = 0b00000000;
//OSCFIP/CMIP/ /EEIP/BCLIP/HLVDIP/TMR3IP/CCP2IP
//PSPIF/ADIF/RCIF/TXIF/SSPIF/CCP1IF/TMR2IF/TMR1IE
//PSPIP/ADIP/RCIP/TXIP/SSPIP/CCP1IP/TMR2IP/TMR1IP
//PSPIF/ADIF/RCIF/TXIF/SSPIF/CCP1IF/TMR2IF/TMR1IF
Ler_Set_Temp:
Lcd_Out(1, 1, "Set Temp: ");
//Exibe Setpoint Temperatura salvo na EEPROM
WREN_bit = 0; //Desabilita ciclo de escrita
na EEPROM
EECON1 = 0b00000001; //EEPGD= 0 / CFGS= 0 /
? / FREE= 0 / WRERR= 0 / WREN= 0 / WR= 0 / RD= 1 //Habilita Leitura EEPROM
if (EEIE_bit == 1 && EEPGD_bit == 0 && CFGS_
bit == 0 && WREN_bit == 0 && WR_bit == 0) {
EEADR = id_ee_sp_temp;
EEDATA = EEPROM_Read(EEADR);
read_ee_sp_temp = EEDATA;
chr_ee_sp_temp[0] = (((read_ee_sp_temp /
100) % 10) + 48); //centena
chr_ee_sp_temp[1] = (((read_ee_sp_temp /
10) % 10) + 48); //dezena
chr_ee_sp_temp[2] = ((read_ee_sp_temp %
10) + 48); //unidade
Lcd_Chr_CP(chr_ee_sp_temp[0]);
Lcd_Chr_CP(chr_ee_sp_temp[1]);
Lcd_Chr_CP('.');
Lcd_Chr_CP(chr_ee_sp_temp[2]);
Lcd_Out_CP(" *C");
EEIF_bit = 0;
}
//Incrementa valor Setpoint Temperatura
if (Button(&PORTD, B_UP, deboucy, 0)) {
if (sp_temp <= sp_min_temp) sp_temp = sp
_min_temp; //Trava Setpoint temperatura no limite minimo permitido
if (sp_temp > sp_min_temp) sp_temp--;
}
//Decrementa valor Setpoint Temperatura
if (Button(&PORTD, B_DOWN, deboucy, 0)) {
if (sp_temp >= sp_max_temp) sp_temp = sp
_max_temp; //Trava Setpoint temperatura no limite maximo permitido
if (sp_temp < sp_max_temp) sp_temp++;
}
//Exibe valor Setpoint Temperatura alterado
++ ou -Lcd_Out(2, 1, "Inc/Dec: ");
chr_sp_temp[0] = (((sp_temp / 100) % 10) + 4
8); //centena
chr_sp_temp[1] = (((sp_temp / 10) % 10) + 4
8); //dezena
//unidade
Lcd_Chr_CP(chr_passwd[0]);
Lcd_Chr_CP(chr_passwd[1]);
Lcd_Chr_CP(chr_passwd[2]);
//Compara senha alterada com gravada
na EEPROM
if (Button(&PORTD, B_ENTER, deboucy,
1)) {
WREN_bit = 0;
//Desabil
EECON1 = 0b00000001;
//EEPGD/C
if (check_passwd == read_ee_
passwd) {
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(3, 4, "Senha cor
reta");
EEIF_bit = 0;
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
EEADR = id_ee_sp_temp;
EEDATA = sp_temp;
WR_EEPROM(EEADR, EEDATA)
;
Delay_ms(1000);
//Setpoint Temperatura
salvo com sucesso na EEPROM
if(success) {
Lcd_Cmd(_LCD_CLEA
R);
WREN_bit = 0; //D
esabilita ciclo de escrita EEPROM
Lcd_Out(3, 1, "Se
t Salvo!");
;
Delay_ms(1000);
goto Ler_Set_Temp
//Salta no inicio do metodo lendo a temperatura da EEPROM
}
//Erro ao salvar Set
EEIE_bit = 1;
EEIF_bit = 0;
//Limpa flag da EEPROM
INTCON = 0b11000000;
//Carrega estado previo salvo de INTCON
cursor_par_ctrl = 1;
op_par_ctrl = 0;
Lcd_Cmd(_LCD_CLEAR);
}