You are on page 1of 22

//BROJANJE SLOVA

#define _XTAL_FREQ 8000000


#define D7 RD7
#define D6 RD6
#define D5 RD5
#define D4 RD4
#define EN RD3
#define RS RD2

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

#pragma config FOSC = HS


#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

#include "led.h"
#include "Uart_com.h"
#include "servo.h"

unsigned int brojac = 0;

void main(void) {

TRISD = 0x00;
TRISB = 0x00;
RB0 = 0;
Lcd_Init();
Initializing_UART(9600);

char a; // char za preuzimanje svakog slova


Lcd_Clear();
Lcd_Set_Cursor(1,1);
while(1){
a = UART_get_char(); // preuzimanje predanog slova
if(a){
// ako je preuzeto slovo, pove?a se brojac za 1
brojac++;
if(brojac%16==0){
//ako je popunjen prvi red LCD displeja
Lcd_Set_Cursor(2,1);
}
if(brojac%(16*2)==0){
//ako su popunjena oba reda displeja
Lcd_Clear();
Lcd_Set_Cursor(1,1);
}
Lcd_Write_Char(a);
}
if(brojac<10){
//broj slova manji od 10, pali se dioda
RB0 = 1;

}
else if( brojac==10){
//broj slova od 10 do 20, pokrece se servo motor
//ovdje sam stavio da se pokrene ako je brojac jednak 10, jer inace
//?e se pokretati motor za svako uneseno slovo
RB0 = 0;
servoRotiraj180();
servoRotiraj90();
}
else if(brojac==20){
//ako je brojac veci od 19, motor radi sta radi
RB0 = 0;
servoRotiraj0();
servoRotiraj90();
}
}
return;
}

///////////////stoperica-master
//stoperica zadatak - master
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

#define _XTAL_FREQ 8000000


#define Baud_rate 9600
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

void Initializing_UART(void){
TRISC6 = 0;
TRISC7 = 1;

SPBRG = ((_XTAL_FREQ/16)/Baud_rate)-1;
BRGH = 1;
SYNC = 0;
SPEN = 1;
TXEN = 1;
CREN = 1;

TX9 = 0;
RX9 = 0;
}

void UART_send_char(char bt){


while(!TXIF);
TXREG = bt;
}

void main(void) {
Initializing_UART();
TRISB = 0b11111111;
int flag = 0;
int flag2 = 0;
while(1){

if(!RB0 && flag == 0 ){


UART_send_char('p');
flag=1;
}
else if(RB0==1){
flag = 0;
}
if(!RB1 && flag2 == 0 ){
UART_send_char('s');
flag2=1;
}
else if(RB1==1){
flag2 = 0;
}

}
return;
}
////////////////////slave-stoperica
//stoperica zadatak - master
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

#define _XTAL_FREQ 8000000


#define Baud_rate 9600
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

void Initializing_UART(void){
TRISC6 = 0;
TRISC7 = 1;

SPBRG = ((_XTAL_FREQ/16)/Baud_rate)-1;
BRGH = 1;
SYNC = 0;
SPEN = 1;
TXEN = 1;
CREN = 1;

TX9 = 0;
RX9 = 0;
}

void UART_send_char(char bt){


while(!TXIF);
TXREG = bt;

void main(void) {
Initializing_UART();
TRISB = 0b11111111;
int flag = 0;
int flag2 = 0;
while(1){

if(!RB0 && flag == 0 ){


UART_send_char('p');
flag=1;
}
else if(RB0==1){
flag = 0;
}
if(!RB1 && flag2 == 0 ){
UART_send_char('s');
flag2=1;
}
else if(RB1==1){
flag2 = 0;
}

}
return;
}

//////////////dc motor
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#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)

#define _XTAL_FREQ 20000000


#define TMR2PRESCALE 4
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"

/////////////////////////////////////

void ADC_Initialize(){
ADCON0 = 0b01000001; //ADC ON and Fosc/16 is selected
ADCON1 = 0b11000000; // Internal reference voltage is selected
}

unsigned int ADC_Read(unsigned char channel)


{
ADCON0 &= 0x11000101; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); //Returns Result
}
////////////////////////////////////

int br1=0;
int count=0;
int hscds = 0;

//////////////////////////////////////////////
long PWM_freq = 5000;

PWM_Init()
{
PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae
using Datasheet // Makes the PWM work in 5KHZ
CCP1M3 = 1; CCP1M2 = 1; //Configure the CCP1 module
T2CKPS0 = 1;T2CKPS1 = 0; TMR2ON = 1; //Configure the Timer module
TRISC2 = 0; // make port pin on C as output
}

PWM_Duty(unsigned int duty)


{
if(duty<1023)
{

duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); // On
reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) *
TMR2PRESCALE);
CCP1X = duty & 1; //Store the 1st bit
CCP1Y = duty & 2; //Store the 0th bit
CCPR1L = duty>>2;// Store the remining 8 bit
}
}
/////////////////////////////////////////
void main() {
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
PORTB = 0x00;
PORTD = 0x00;

PWM_Init();
Lcd_Init();
ADC_Initialize();

int i = 0;
count = 0;
while(1){
i = ADC_Read(2);
PWM_Duty(i);
count++;
if(count==400){ //ispis na lcd brzine motora
float brzina = i*2.929;
/*int brzina_zarez = (int)brzina;
brzina_zarez = brzina_zarez%100;*/
//int prijeZareza = (int)brzina;
/* int poslijeZareza = brzina_zarez;*/
char prije[4];
char poslije[4];
sprintf(prije,"%d",(int)brzina);
//sprintf(poslije,"%d",poslijeZareza);
Lcd_Clear();
Lcd_Set_Cursor(2,1);
Lcd_Write_Char(prije[0]);
Lcd_Write_Char(prije[1]);
Lcd_Write_Char(prije[2]);
Lcd_Write_Char(prije[3]);
//Lcd_Write_String(poslije);
__delay_ms(300);
count=0;
}

}
return;
}

////////////////vrijeme-ispis
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

#define _XTAL_FREQ 200000000


#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#include <xc.h>
#include "led_biblioteka.h"

int count = 0;
char hscnd = 0;
int count2 = 0;
char hscnd2 = 0;
int br1 = 0;

unsigned int Vrijednost(int broj){


unsigned int rezultat;
unsigned int Segment_Display[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D,
0x07, 0x7F,0x6F};

return Segment_Display[broj];
}

__interrupt() void timer_isr(void)


{
if(TMR0IF==1) // Timer flag has been triggered due to timer overflow
{
TMR0 = 100; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
count++;
count2++;
}

if (count == 250)
{
hscnd+=1; // hscnd will get incremented for every half second
count=0;

}
if(hscnd==2){
br1++;
hscnd=0;
}

if(count2==2){
hscnd2+=1;
count2=0;
}

}
void diode(int a, int b, int c){
RC6 = a;
RC5 = b;
RC4 = c;
}

void main(){
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
int jedinice, desetice, stotice, hiljade;
PORTB = 0xFF;
OPTION_REG = 0B00000101;
TMR0 = 100;
TMR0IE = 1;
GIE = 1;
PEIE = 1;

Lcd_Init();
RC6 = 0;
RC5 = 0;
RC4 = 0;

while(1){
if(br1 == 7){
diode(1, 0, 0);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("00:07 Vrijeme je");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("pod kontrolom!");
}
if(br1 == 11){
diode(0, 1, 0);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("00:11 Vrijeme");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("je na granici!");
}
if(br1 == 22222){
diode(0, 0, 1);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("Vrijeme je izvan");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("dozvoljene granice!");

for(int i = 0; i<5; i++){


__delay_ms(5);
Lcd_Shift_Right();
}

for(int i =0; i<5; i++){


__delay_ms(5);
Lcd_Shift_Left();
}
}

if(br1== 23*60+59){
br1=0;
}

//for (int i =0; i<5; i++){


//jedinice
if(hscnd2==1){
jedinice = br1 % 10;
PORTB = Vrijednost(jedinice);
RC3 = 0;
}else if(hscnd2==3 ){
RC3 = 1;
//desetice
desetice = br1/10%6;
PORTB = Vrijednost(desetice);
RC2 = 0;
}
else if(hscnd2==5){
RC2 = 1;RC3=1;RC1=0;RC0=1;
stotice = br1/60%10;
PORTB = Vrijednost(stotice);
}
else if(hscnd2==8){
RC2 = 1;RC3=1;RC1=1;RC0=0;
hiljade = br1/600%10;
PORTB = Vrijednost(hiljade);
}
else if(hscnd2==10){
RC2 = 1;RC3=1;RC1=1;RC0=1;
PORTD = 0x00;
hscnd2=0;
count2=0;
}
}
}

// }

///////////////viertuelni opcije
#define _XTAL_FREQ 20000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
void UART_Init(const long int baudrate) {
TRISC7 = 1;
TRISC6 = 0;

SPBRG = ((_XTAL_FREQ/16)/baudrate)-1;
BRGH = 1;
SYNC = 0;
SPEN = 1;
CREN = 1;
TXEN = 1;

TX9 = 0;
RX9 = 0;
}

void UART_Send_Char(char bt) {


while(!TXIF);
TXREG = bt;
}

char UART_Get_Char() {
if(OERR) {
CREN = 0;
CREN = 1;
}
while(!RCIF);
return RCREG;
}

char UART_Send_String(char* st_pt) {


while(*st_pt) // ako ima char
UART_Send_Char(*st_pt++);
}
/////////////////////////////////////

void servo0() {
unsigned int i;
for(i = 0; i < 50; i++) {
RB0 = 1;
__delay_us(800);
RB0 = 0;
__delay_us(19200);
}
}

void servo180() {
unsigned int i;
for(i = 0; i < 50; i++) {
RB0 = 1;
__delay_us(2200);
RB0 = 0;
__delay_us(17800);
}
}

////////////////////////////////////
int count = 0;
char hscnd = 0;

int br1 = 0;
__interrupt() void timer_isr(void)
{
if(TMR0IF==1) // Timer flag has been triggered due to timer overflow
{
TMR0 = 100; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
count++;
}

if (count == 250)
{
hscnd+=1; // hscnd will get incremented for every half second
count=0;
}
if(hscnd==2){
br1++;
hscnd=0;
}

}
void ADC_Initialize(){
ADCON0 = 0b01000001; //ADC ON and Fosc/16 is selected
ADCON1 = 0b11000000; // Internal reference voltage is selected
}

unsigned int ADC_Read(unsigned char channel)


{
ADCON0 &= 0x11000101; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); //Returns Result
}
////////////////////////////////////
int adc=0;int adc1=0; float i = 0;float k = 0;
void opcija1(){
//LM35
/*char pokaz[8];
adc = (ADC_Read(0));
adc1 = (ADC_Read(1));
i = adc*0.00488281; // LM35 ulaz
k = adc1*0.00488281;
i=(i-k)*100;
if(i<0)i=i-1;
sprintf(pokaz,"%d",(int)i);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Temp je: ");

Lcd_Set_Cursor(2,1);
Lcd_Print_String(pokaz);
Lcd_Print_String(" C");*/

//NTC
char prvi1[8];
char drugi1[8];
adc = (ADC_Read(2));
i = adc*0.00488281; // ntc ulaz
k = i;
if(i>=4.6 || i<=0.2){
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Temp izvan opsega");
return;
}
if(i>=1.14){
i = i - 1.14;
k = i*27;
}
int prvi = (int)k;
float bb = i*100;
int drugi =(int)bb;
sprintf(prvi1,"%i",prvi);
sprintf(drugi1,"%i",drugi%100);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Temp je: ");
Lcd_Set_Cursor(2,1);
Lcd_Print_String(prvi1);
Lcd_Print_String(",");
Lcd_Print_String(drugi1);
Lcd_Print_String(" C");
__delay_ms(1000);
Lcd_Clear();
}
int jedinice, desetice, stotice, hiljade;
void opcija2(){
jedinice = br1 % 10;
desetice = br1/10%6;
stotice = br1/60%10;
hiljade = br1/600%10;
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Vrijeme je: ");
Lcd_Set_Cursor(2,1);
Lcd_Print_Char(48+hiljade);
Lcd_Print_Char(48+stotice);
Lcd_Print_Char(':');
Lcd_Print_Char(48+desetice);
Lcd_Print_Char(48+jedinice);
__delay_ms(1000);
Lcd_Clear();

}
void opcija3(){
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Odabrana opcija:");
Lcd_Set_Cursor(2,1);
Lcd_Print_String("3");
__delay_ms(1000);
Lcd_Clear();

}
void opcija4(){
servo0();
RB1 = 1;
servo180();
RB1 = 0;

}
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#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)

void menuShow(){
UART_Send_String("\r");
UART_Send_String("***OPCIJE*** \r");
UART_Send_String("***OPCIJA 1: Ispis trenutne temp sa senzora*** \r");
UART_Send_String("***OPCIJA 2: Ispis trenutnog vremena*** \r");
UART_Send_String("***OPCIJA 3: Ispis na LCD ovo je opcija 3*** \r");
UART_Send_String("***OPCIJA 4: Okretanje servo motora na jednu str, dioda ON***
\r");
UART_Send_String("****************************************** \r");
UART_Send_String("***Molim Vas odaberite opciju 1,2,3,4*** \r");
UART_Send_String("******************************************* \r");

void main() {
TRISB = 0x00;
TRISD = 0x00;
PORTB = 0x00;
PORTD = 0x00;

OPTION_REG = 0B00000101;
TMR0 = 100;
TMR0IE = 1;
GIE = 1;
PEIE = 1;

int vrijednost = 0;
UART_Init(9600);
Lcd_Start();
ADC_Initialize();
UART_Send_String("**->Donrodosli ! Ovo je drugi zad, Ivan Ivusic<-***\r");

while(1){
menuShow();
vrijednost = UART_Get_Char();
switch(vrijednost) {
case '1':
UART_Send_String("Odabrali ste opciju 1 \r");
UART_Send_String("Kupljenje temperature \r");
opcija1();
break;
case '2':
UART_Send_String("Odabrali ste opciju 2 \r");
UART_Send_String("Kupljenje trenutnog vremena \r");
opcija2();
break;
case '3':
UART_Send_String("Odabrali ste opciju 3 \r");
UART_Send_String("Ispis na LCD \r");
opcija3();
break;
case '4':
UART_Send_String("Odabrali ste opciju 4 \r");
UART_Send_String("Okretanje motora, te paljenje diode \r");
opcija4();
__delay_ms(300);
break;
}
}
return;
}

/////////////////alarm

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS 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 = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming
enabled)
#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 conf
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>
#define _XTAL_FREQ 20000000
#include "uart.h"
#include "lcd.h"

//////////////////////////////////////////////
unsigned int Vrijednost(int broj){
unsigned int rezultat;
unsigned int Segment_Display[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D,
0x07, 0x7F,0x6F};

return Segment_Display[broj];
}

/////////////////////////////////////////////////
int pocetak_brojanja=0;
int count = 0;int br=0;
int count2 = 0; int br2=0;
int flag3 = 0;
__interrupt() void timer(){
if(TMR0IF == 1 ){
TMR0 = 100;
TMR0IF = 0;
if(pocetak_brojanja==1){
count++;
count2++;
}
}
if(count==501){
br++;
count=0;
Lcd_Set_Cursor(1,1);
Lcd_Print_Char(br/600%10+48);
Lcd_Print_Char(br/60%10+48);
Lcd_Print_Char(':');
Lcd_Print_Char(br/10%6+48);
Lcd_Print_Char(br%10+48);
}
if(flag3==1){
RC4=1;
}else {RC4=0;}

}
//////////////////////////////////////////////////

void main(void) {
TRISB=0x00;
TRISD=0x00;
TRISC0=0;
TRISC1=0;
TRISC2=0;
TRISC3=0;
TRISC4=0;

PORTB = 0x00;
RC0 = RC1 = RC2 = RC3=0; RC4 = 0;

UART_Init();
Lcd_Start();

OPTION_REG = 0B00000101;
TMR0 = 100;
TMR0IE = 1;
GIE = 1;
PEIE = 1;

Lcd_Clear();

char pocetno_vrijeme[5];
char vrijeme_alarma[5];
char odgovor;
int flag = 1;
int flag2 = 1;

int poc;
int alarm;

UART_send_string("pobrodosli u Virtualni terminal \r");


while(flag){
if(flag2==1){
UART_send_string("Dobrodosli u Virtualni terminal \r");
UART_send_string("Molim Vas podesite sat: \r");
UART_send_string("Korisnik: ");

pocetno_vrijeme[0]=UART_get_char();
pocetno_vrijeme[1]=UART_get_char();
pocetno_vrijeme[2]=UART_get_char();
pocetno_vrijeme[3]=UART_get_char();
pocetno_vrijeme[4]=UART_get_char();

UART_send_string("\rTrenutno vrijeme je: ");


UART_send_char(pocetno_vrijeme[0]);
UART_send_char(pocetno_vrijeme[1]);
UART_send_char(pocetno_vrijeme[2]);
UART_send_char(pocetno_vrijeme[3]);
UART_send_char(pocetno_vrijeme[4]);
UART_send_string("\r");

poc = pocetno_vrijeme[4]-'0'+(pocetno_vrijeme[3]-48)*10+
(pocetno_vrijeme[1]-48)*60+(pocetno_vrijeme[0]-48)*10*60;
br = poc;
pocetak_brojanja = 1;
}
flag2=0;
UART_send_string("\r");
UART_send_string("Molim Vas podesite alarm: \r");
UART_send_string("Korisnik: ");

vrijeme_alarma[0]=UART_get_char();
vrijeme_alarma[1]=UART_get_char();
vrijeme_alarma[2]=UART_get_char();
vrijeme_alarma[3]=UART_get_char();
vrijeme_alarma[4]=UART_get_char();
UART_send_string("\r");

UART_send_string("Alarm je podesen na vrijeme: ");


UART_send_char(vrijeme_alarma[0]);
UART_send_char(vrijeme_alarma[1]);
UART_send_char(vrijeme_alarma[2]);
UART_send_char(vrijeme_alarma[3]);
UART_send_char(vrijeme_alarma[4]);
UART_send_string("\r");
alarm = vrijeme_alarma[4]-'0'+(vrijeme_alarma[3]-48)*10+(vrijeme_alarma[1]-
48)*60+(vrijeme_alarma[0]-48)*10*60;

while(alarm>=br){
}
flag3=1;
RB0=1;
RB1 = 0;
UART_send_string("\r");
UART_send_string("Alarm je aktiviran \r");

UART_send_string("Da li zelite podesiti novo vrijeme? y/n");


UART_send_string("\r");
while(!RCIF){
if(br>=alarm+10){
RB0 = 0;
RB1 = 1;
}
if(br>=alarm+15){
RB0 = 0;
RB1 = 0;
flag3=0;
}

}
RB0 = 0;
RB1 = 0;
odgovor = UART_get_char();
flag3=0;
if(odgovor=='y'){continue;}
else {break;}
UART_send_string("\r");
}
return;
}

///////////////////////////zadnji rok
//ALRAM ZASTITE KUCE

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS 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 = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming
enabled)
#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)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.
# define D7 RD7
# define D6 RD6
# define D5 RD5
# define D4 RD4
# define EN RD3
# define RS RD2
#define _XTAL_FREQ 20000000

#include <xc.h>
#include "uart.h"
#include "lcd.h"

int count=0;int br=0;


int alarmPokretanje = 0;
__interrupt() void tajmer(){
if(TMR0IF==1){
TMR0IF=0;
TMR0 = 100;
if(alarmPokretanje==1){
count++;
}
}
if(count==501){
br++;
Lcd_Set_Cursor(2,1);
Lcd_Print_Char(br+'0');
count=0;
}

void alarm(){
RB1 = 1;
__delay_ms(500);;
RB1 = 0;
__delay_ms(500);;
RB1 = 1;
__delay_ms(500);;
RB1 = 0;
__delay_ms(500);;
RB1 = 1;
__delay_ms(500);;
RB1 = 0;
__delay_ms(500);;
RB1 = 1;
__delay_ms(500);;
RB1 = 0;
__delay_ms(500);;
RB1 = 1;
__delay_ms(500);;
RB1 = 0;
}

void main(){
OPTION_REG = 0b00000101;
TMR0 = 100;
PEIE = 1;
GIE = 1;
TMR0IE = 1;

TRISD = 0x00;
TRISB0 = 0;
TRISB1 = 0;
TRISB2 = 1;

PORTD = 0x00;
PORTB = 0x00;

char sifra[5];
Lcd_Start();
UART_Init();

UART_send_string("Dobrodsli u sistem zastite vase kuce!\r");


Lcd_Set_Cursor(1,1);
Lcd_Print_String("Sistem je upaljen");

int flag = 0;
int flag2 = 0;
int flagAktivacija=0;

while(1){
if(RB2 == 0 flag ==0){
UART_send_string("Upisite sifru:\r");
sifra[0] = UART_get_char();
sifra[1] = UART_get_char();
sifra[2] = UART_get_char();
sifra[3] = UART_get_char();
sifra[4] = UART_get_char();
UART_send_string("\r");
flag = 1;
}

if(flag==1 && sifra[0]=='1' && sifra[1]=='2' && sifra[2]=='3' &&


sifra[3]=='4' && sifra[4]=='5'){
flagAktivacija = 1;
}
else if (flag==1){
flagAktivacija = 0;
flag = 0;
flag2=0;
UART_send_string("Pogresno, unesi ponovo\r");
continue;
}

if(RB2 == 1 && RCIF && flagAktivacija == 0){


UART_get_char();
}

if(flagAktivacija && RB2==0){


UART_send_string("Sifra je ispravna!\r");
UART_send_string("Sistem za zastitu kuce je aktiviran!\r");
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Sistem je aktiviran");

while(1){
if(flag==0){break;}
if(flag2==1){break;}
if(RB2 == 1){
int i = 1;

UART_send_string("Uljez u kuci\r");
UART_send_string("Unesite sifru za deaktivaciju, imate 10
sec!\r");

Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Uljez je u kuci!");
sifra[0]=0;
sifra[1]=0;
while(1){
alarmPokretanje=1;
if(RCIF==1 && i==1){
sifra[0] = UART_get_char();
i++;
}
if(RCIF==1 && i==2){
sifra[1] = UART_get_char();
i++;
}
if(RCIF==1 && i==3){
sifra[2] = UART_get_char();
i++;
}
if(RCIF==1 && i==4){
sifra[3] = UART_get_char();
i++;
}
if(RCIF==1 && i==5){
sifra[4] = UART_get_char();
i++;
}

if(br==10){
alarmPokretanje=0;
br=0;
flag2=1;
UART_send_string("\rALARM!\r");
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Alarm se aktiviran");
alarm();
break;
}
if(sifra[0]=='1' && sifra[1]=='2' && sifra[2]=='3' &&
sifra[3]=='4' && sifra[4]=='5' && i==6){
alarmPokretanje=0;
flagAktivacija = 0;
flag=0;
UART_send_string("\rSifra je ispravna!\r");
UART_send_string("Sve je pod kotrolom!\r");
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Sistem je sigurno");
break;

}
else if(i==6 || br==10){
alarmPokretanje=0;
UART_send_string("\rSifra je pogresna!\r");
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Alarm se aktivira");
alarm();
flag2=1;
break;
}
}
}
}
}

}
Lcd_Set_Cursor(1,1);
Lcd_Print_String("KRAJJJ");
return;
}

//////////////////////dht
#define _XTAL_FREQ 8000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#define DHT11_PORT RB0


#define DHT11_TRIS TRISB0

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "bibliotekaDHT11.h"
#include "bibliotekaLCD.h"

#pragma config FOSC = HS // Oscillator Selection bits (HS 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 = ON // Brown-out Reset Enable bit (BOR enabled)
#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)

char Temperatura[] = "Temp = 00.0 C ";


char Vlaznost[] = "Vlaz = 00.0 % ";
unsigned char V1, V2, T1, T2, KontrolnaSuma;

void main() {
TRISD = 0x00;
PORTD = 0x00;
TRISB = 0x00;
PORTB = 0x00;
T1CON = 0x10;
TMR1H = 0;
TMR1L = 0;
Lcd_Init();
Lcd_Clear();

while(1) {
Start_Signal();
if(Check_Response()) {
if(Read_Data(&V1) || Read_Data(&V2) || Read_Data(&T1) || Read_Data(&T2)
|| Read_Data(&KontrolnaSuma)) {
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("Time out!");
} else {
if(KontrolnaSuma == ((V1+V2+T1+T2) & 0xFF)) {
Temperatura[7] = T1/10+'0';
Temperatura[8] = T1%10+'0';
Temperatura[10] = T2/10+'0';
Temperatura[11] = 223;

Vlaznost[7] = V1/10+'0';
Vlaznost[8] = V1%10+'0';
Vlaznost[10] = V2/10+'0';

Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String(Temperatura);
Lcd_Set_Cursor(2,1);
Lcd_Write_String(Vlaznost);
}
}
} else {
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("Nema konekcije");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("sa senzorom");
}

TMR1ON = 0;
__delay_ms(1000);
}
}

You might also like