povilas
Junior Member level 1
- Joined
- Mar 4, 2013
- Messages
- 17
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,382
Hi, I am using mikroc Pro for pic. pic18f4550. And know I connected 2 projects in one project. DS18S20 thermometers and RTC. But know program is running very slow. If DS18S20 is working RTC not. I find out I need interrupts, but I am new in this and don't understant how use them, can anyone help?
here is my source code:
Povilas.
here is my source code:
Code:
// osc 8MHz
// mikroC PRO for PIC 6.0.1
//PIC18F4520
// DS18S20 and RTC project
// LCD ekrano prijungimai
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
#define RELAY0 RD0
#define RELAY1 RD1
#define RELAY2 RD2
#define RELAY3 RD3
#define RELAY4 RD4
#define DS1307_HWAddress 0xD0 // Hardware address of the RTC
#ifndef HIGH
#define HIGH 1
#define LOW 0
#endif // HIGH + LOW
char addr[7][8] = {{0x10,0x30,0xC5,0xC8,0x00,0x00,0x00,0xC3}, //katilas
{0x10,0x31,0xC5,0xC8,0x00,0x00,0x00,0xF4}, //griztamasis
{0x10,0x32,0xC5,0xC8,0x00,0x00,0x00,0xAD}, // karsta
{0x10,0x33,0xC5,0xC8,0x00,0x00,0x00,0x9A}, //kamb
{0x10,0x34,0xC5,0xC8,0x00,0x00,0x00,0x1F}, //akumulicinis
{0x10,0x35,0xC5,0xC8,0x00,0x00,0x00,0x28}, //saule
{0x10,0x36,0xC5,0xC8,0x00,0x00,0x00,0x71}}; //laukas
unsigned char bSec, bMin, bStd, bDay, bMon, bYear, oldstate;
unsigned int uSec, uMin, uStd, uDay, uMon, uYear;
unsigned char output[21];
extern void menu( void);
extern void StoreToRTC( void);
extern void InitDisplay( void);
extern void Set_Position( unsigned char Sign);
extern void IncDecSign( unsigned Sign, unsigned direct);
extern unsigned char DS1307_Read(unsigned char Addr);
extern void DS1307_Write(unsigned char Addr, unsigned char Data);
int i,j ;
char katil, grizt, karst, kamb, akum, saule, laukas ;
const unsigned short TEMP_RESOLUTION = 9;// nustatomas konstantos tipas ir sensoriaus tipas
char *text = "000.0"; //nustatomas kintamojo tipas ir pradin? jo reikšm?
unsigned temp; //kintamasis temperat?ros išvedimui
unsigned char DS1307_Read(unsigned char Addr)
{
unsigned char AddrBuff;
AddrBuff=DS1307_HWAddress;
I2C1_Start(); // I2C pradejimas
I2C1_Wr(AddrBuff); // Siusti itaiso adresa
I2C1_Wr(Addr); // Siusti duomenis
I2C1_Repeated_Start();
AddrBuff.F0=HIGH; // Konfiguruoti duomenu bitus skaitymui
I2C1_Wr(AddrBuff); // Siusti itaiso adresa
AddrBuff=I2C1_Rd(0); // Read the byte from the slave and NACK to end comms
I2C1_Stop(); // Free up the bus
return(AddrBuff);
}
void DS1307_Write(unsigned char Addr, unsigned char Data)
{
I2C1_Start(); // Issue I2C start signal
I2C1_Wr(DS1307_HWAddress); // Send hardware address for writting
I2C1_Wr(Addr); // Send low byte of the data address
I2C1_Wr(Data); // Send the data
I2C1_Stop(); // Free the bus
return;
}
void InitDisplay( void)
{
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn off cursor
Lcd_Out( 1, 1, "Laikas: 00:00:00");
Lcd_Out( 2, 1, "Data: 0000.00.00");
Delay_ms( 100);
}
void Set_Position( unsigned char Sign)
{
switch( Sign)
{
case 0: uStd = (unsigned int)bStd;
sprinti(&output, "%02u", uStd);
Lcd_Out( 1, 9, output);
Lcd_Out( 1, 10, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
case 1: uMin = (unsigned int)bMin;
sprinti(&output, "%02u", uMin);
Lcd_Out( 1, 12, output);
Lcd_Out( 1, 13, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
case 2: uSec = (unsigned int)bSec;
sprinti(&output, "%02u", uSec);
Lcd_Out( 1, 15, output);
Lcd_Out( 1, 16, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
case 3: uYear = (unsigned int)bYear + 2010;
sprinti(&output, "%04u", uYear);
Lcd_Out( 2, 7, output);
Lcd_Out( 2, 10, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
case 4: uMon = (unsigned int)bMon;
sprinti(&output, "%02u", uMon);
Lcd_Out( 2, 12, output);
Lcd_Out( 2, 13, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
case 5: uDay = (unsigned int)bDay;
sprinti(&output, "%02u", uDay);
Lcd_Out( 2, 15, output);
Lcd_Out( 2, 16, "");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
break;
};
}
void IncDecSign( unsigned Sign, unsigned direct)
{
switch( Sign)
{
case 0: if( !direct)
{
bStd++;
if( bStd > 23)
bStd = 23;
} else {
if( bStd > 0)
bStd--;
};
uStd = (unsigned int)bStd;
break;
case 1: if( !direct)
{
bMin++;
if( bMin > 59)
bMin = 59;
} else {
if( bMin > 0)
bMin--;
};
uMin = (unsigned int)bMin;
break;
case 2: if( !direct)
{
bSec++;
if( bSec > 59)
bSec = 59;
} else {
if( bSec > 0)
bSec--;
};
uSec = (unsigned int)bSec;
break;
case 3: if( !direct)
{
bYear++;
if( bYear > 99)
bYear = 99;
} else {
if( bYear > 0)
bYear--;
};
uYear = (unsigned int)bYear + 2010;
break;
case 4: if( !direct)
{
bMon++;
if( bMon > 12)
bMon = 12;
} else {
if( bMon > 0)
bMon--;
};
uMon = (unsigned int)bMon;
break;
case 5: if( !direct)
{
bDay++;
if( bDay > 31)
bDay = 31;
} else {
if( bDay > 0)
bDay--;
};
uDay = (unsigned int)bDay;
break;
};
}
void StoreToRTC( void)
{
Lcd_Cmd(_Lcd_CLEAR);
Lcd_Cmd(_Lcd_CURSOR_OFF);
Lcd_Out( 1, 1, "Nustatyta=)");
bSec = (((bSec / 10) << 4) & 0xF0) + ((bSec-(bSec / 10) * 10) & 0b00001111);
DS1307_Write( 0x00, bSec);
bMin = (((bMin / 10) << 4) & 0xF0) + ((bMin-(bMin / 10) * 10) & 0b00001111);
DS1307_Write( 0x01, bMin);
bStd = (((bStd / 10) << 4) & 0xF0) + ((bStd-(bStd / 10) * 10) & 0b00001111);
DS1307_Write( 0x02, bStd);
// bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);
bDay = (((bDay / 10) << 4) & 0x30) + ((bDay-(bDay / 10) * 10) & 0b00001111);
DS1307_Write( 0x04, bDay);
bMon = (((bMon / 10) << 4) & 0x10) + ((bMon-(bMon / 10) * 10) & 0b00001111);
DS1307_Write( 0x05, bMon);
bYear = (((bYear / 10) << 4) & 0xC0) + ((bYear-(bYear / 10) * 10) & 0b00001111);
DS1307_Write( 0x06, bYear);
delay_ms( 2000);
}
// KeyPort, input via button
//////////////////////////////
// F0 = Menu/Exit
// F1 = Select
// F2 = INC Number
// F3 = DEC Number
// F7 = Reset Clock-Chip
void menu( void)
{
unsigned char Sig;
Sig = 0;
//Lcd_Out( 1, 1, " ");
Lcd_Out( 1, 1, "Nustat");
Lcd_Out( 2, 1, "yti ");
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
Set_Position( Sig);
Delay_ms( 100);
do
{
// Menu key pressed?
////////////////////
if (Button(&PORTA, 0, 1, 0)) {
//delay_ms( 250);
break;
};
if(Button(&PORTC, 6, 1, 0)) {
Sig++;
if( Sig > 5)
Sig = 0;
Set_Position( Sig);
}
// Increment
if (Button( &PORTD, 5, 1, 0)) {
delay_ms( 100);
IncDecSign( Sig, 0);
Set_Position( Sig);
};
// Decrement
if(Button( &PORTD, 6, 1, 0)) {
delay_ms( 100);
IncDecSign( Sig, 1);
Set_Position( Sig);
};
Delay_ms( 100);
} while( 1);
// Store all Settings in DS1307
///////////////////////////////
StoreToRTC();
// Exit to Clock
Lcd_Cmd(_Lcd_CURSOR_OFF);
// Refresh Display
InitDisplay();
}
void laikrodis (){
oldstate = 0;
delay_ms( 100);
bSec = DS1307_Read( 0x00);
bSec = ((bSec & 0xF0) >> 4)*10 + (bSec & 0b00001111);
uSec = (unsigned int)bSec;
bMin = DS1307_Read( 0x01);
bMin = ((bMin & 0xF0) >> 4)*10 + (bMin & 0b00001111);
uMin = (unsigned int)bMin;
bStd = DS1307_Read( 0x02);
bStd = ((bStd & 0xF0) >> 4)*10 + (bStd & 0b00001111);
uStd = (unsigned int)bStd;
bDay = DS1307_Read( 0x04);
bDay = ((bDay & 0x30) >> 4)*10 + (bDay & 0b00001111);
uDay = (unsigned int)bDay;
bMon = DS1307_Read( 0x05);
bMon = ((bMon & 0x10) >> 4)*10 + (bMon & 0b00001111);
uMon = (unsigned int)bMon;
bYear= DS1307_Read( 0x06);
bYear = ((bYear & 0xC0)>> 4)*10 + (bYear & 0b00001111);
uYear = (unsigned int)bYear + 2010;
sprinti(&output, "%02u:%02u:%02u", uStd, uMin, uSec);
Lcd_out( 1, 9 , output);
sprinti(&output, "%04u.%02u.%02u", uYear, uMon, uDay);
Lcd_out( 2, 7 , output);
//InitDisplay();
// Menu key pressed?
////////////////////
if(Button(&PORTA, 0, 1, 0)) {
delay_ms( 10);
menu(); } }
void Display_Temp(unsigned int temp2write) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8; //priklauso nuo sensoriaus //rezoliucijos
char temp_visa;
unsigned int temp_dal;
int neigtemp = 1;
// Tikrinti ar temperat?ra n?ra neigiama
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
neigtemp=1;
}
else {
neigtemp=0;
}
// gauti kintam?ji temp_visa
temp_visa = temp2write >> RES_SHIFT ;
if (!neigtemp) { // tikrinti kintamaj? neigtemp
if (temp_visa/100) // Perversti temp_visa ? vienetus
text[0] = temp_visa/100 + 48;
else
text[0] = ' ';
}
text[1] = (temp_visa/10)%10 + 48; // Gauti dešim?i? skai?i?
text[2] = temp_visa%10 + 48; // Gauti vienet? skai?i?
// Gauti kintamojo temp_dal vienetus po kablelio
temp_dal = temp2write << (4-RES_SHIFT); // rodo dešimt?sias dalis
temp_dal &= 0x000F;
temp_dal *= 625;
// Paversti kintamojo temp_dal duomenis ? vienetus
text[4] = temp_dal/1000 + 48; // Gauti vienet? skai?i? po kablelio
// Išvesti temperat?ra ? LCD ekran?.
//Lcd_Out(1, 6,text);
if (j==0){
katil = temp;
}
if (j==1){
//Lcd_Out(2, 6,text);
grizt = temp;
}
if (j==2){
//Lcd_Out(3, 6,text);
karst = temp;
}
if (j==3){
// Lcd_Out(4, 6,text);
kamb = temp;
}
if (j==4){
//Lcd_Out(2, 11,text);
akum = temp;
}
if (j==5){
//Lcd_Out(3,11,text);
saule = temp; }
if (j==6){
//Lcd_Out(4, 11,text);
laukas = temp;
}
}
reles (){
if(katil < grizt)
{
PORTD.RELAY0=1;
}
else {
PORTD.RELAY0=0;
}
if (grizt > 50 && PORTD.RELAY0 == 1){
// Lcd_Out(3, 1, "else:");
PORTD.RELAY1=1;
}
if (karst>40){
PORTD.RELAY1=0;
}
if (PORTD.RELAY1 == 1 && kamb < 40)
{
PORTD.RELAY2=1;
}
else {
PORTD.RELAY2=0;
}
if (PORTD.RELAY0==1 && PORTD.RELAY1==1 && PORTD.RELAY2==1){
PORTD.RELAY3=1;
}
else {
PORTD.RELAY3=0;
}
if (grizt<saule){
PORTD.RELAY4=1;
} }
int cnt = 0; // Global variable cnt
void interrupt()
{
//Check if it is TMR0 Overflow ISR
if(TMR0IE && TMR0IF)
{
//TMR0 Overflow ISR
cnt++; //Increment Over Flow Counter
// LCD inicializavimas
}}
void main() {
ADCON1 |= 0x0F; // Configure AN pins as digital
CMCON |= 7;
//PORTC = 0;
PORTD = 0;
TRISC = 0b11000001;
TRISB = 0b00000000;
TRISD = 0b11100000;
TRISA = 0b11100001;
//TMR0IE=1;
I2C1_Init(100000); // initialize I2C
I2C1_Start(); // issue start signal
I2C1_Wr(0xD0); // address DS1307
I2C1_Wr(0); // start from word at address 0
I2C1_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C1_Stop(); // issue stop signal
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn off cursor
Lcd_Out( 1, 1, "Laikas: 00:00:00");
Lcd_Out( 2, 1, "Data: 0000.00.00");
Delay_ms( 100);
//Lcd_Init(); // Inicializuoti LCD
//Lcd_Cmd(_LCD_CLEAR); // Išvalyti LCD
// Lcd_Cmd(_LCD_CURSOR_OFF); // Išjungti kursori?
// Lcd_Out(1, 1, "Temp:");
// Išvedamas laipsnio ženklas ir Celsijaus simbolis
// Lcd_Chr(1,11,223);
//Lcd_Chr(1,12,'C');
//Duomen? nuskaitymo programa
do {
//Temperat?ros nuskaitymas
for( j = 0; j<=6;j++)
{
Ow_Reset(&PORTB, 6);
// Vieno laido perkrovimo signalas
Ow_Write(&PORTB, 6,0x55);
for( i = 0; i<=7;i++ )
{
Ow_Write(&PORTB, 6,addr[j][i]);
} // Visi sistemoje funkcionuojantys DS18S20 vykdys //tà paèià po ðios esanèià komandà.
Ow_Write(&PORTB, 6, 0x44); // DS18S20 formuoja keitimo bûsenos signalà
Delay_us(800);
Ow_Reset(&PORTB, 6);
Ow_Write(&PORTB, 6, 0x55);
for( i = 0; i<=7;i++ )
{
Ow_Write(&PORTB, 6,addr[j][i]);
}
Ow_Write(&PORTB, 6, 0xBE);
// DS18S20 atiduoda devynis duomenø baitus á kontrolerá
temp = Ow_Read(&PORTB, 6);
// Lcd_Out(3, 1, &temp);
temp = (Ow_Read(&PORTB, 6) << + temp;
//Temperatûros iðvedimas i LCD
Display_Temp(temp);
//Delay_ms(120);
reles(); // relay function
}
laikrodis (); RTC function
} while (1);
}
Povilas.
Last edited by a moderator: