Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Zadaci PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

HS, ON, OFF,ON,OFF,OFF,OFF,OFF

#define _XTAL_FREQ 8000000

#define crvena RB6

#define zelena RB7

#define button RC0

void RotacijaMinus90(){

unsigned int i;

for(i=0;i<50;i++){

RB0=1;

__delay_us(800);

RB0=0;

__delay_us(19200);

}}
void RotacijaPlus90(){

unsigned int i;

for(i=0;i<50;i++){

RB0=1;

__delay_us(2200);

RB0=0;

__delay_us(17800);

}}

void main(void){

TRISB=0;

TRISC=1;

while(1){

if(button==0){

crvena=1;

zelena=0;

RotacijaPlus90();}

else{

crvena=0;

zelena=1;

RotacijaMinus90();

}}}
Bibl.h

void Lcd_Port(char a){

if(a & 1) D4 = 1;

else D4 = 0;

if(a & 2) D5 = 1;

else D5 = 0;

if(a & 4) D6 = 1;

else D6 = 0;

if(a & 8) D7 = 1;

else D7 = 0;

/*#define RS RD2

#define EN RD3

#define D4 RD4

#define D5 RD5
#define D6 RD6

#define D7 RD7*/

void Lcd_Cmd(char a){

RS = 0;

Lcd_Port(a);

EN = 1;

__delay_ms(4);

EN = 0;

void Lcd_Clear(){

Lcd_Cmd(0);

Lcd_Cmd(1);

void Lcd_Set_Cursor(char a, char b){

char temp ,z,y;

if(a == 1){

temp = 0x80 + b - 1;

z = temp >> 4;

y = temp & 0x0F;

Lcd_Cmd(z);

Lcd_Cmd(y);

}else if(a == 2){

temp = 0xC0 + b - 1;

z = temp >> 4;

y = temp & 0x0F;

Lcd_Cmd(z);

Lcd_Cmd(y);

}}

void Lcd_Init(){

Lcd_Port(0x00);

__delay_ms(20);
Lcd_Cmd(0x03);

__delay_ms(5);

Lcd_Cmd(0x03);

__delay_ms(11);

Lcd_Cmd(0x03);

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

Lcd_Cmd(0x02);

Lcd_Cmd(0x02);

Lcd_Cmd(0x08);

Lcd_Cmd(0x00);

Lcd_Cmd(0x0C);

Lcd_Cmd(0x00);

Lcd_Cmd(0x06);

void Lcd_Write_Char(char a){

char temp, y;

temp = a&0x0F;

y = a&0xF0;

RS = 1;

Lcd_Port(y >> 4);

EN = 1;

__delay_us(40);

EN = 0;

Lcd_Port(temp);

EN = 1;

__delay_us(40);

EN = 0;}

void Lcd_Write_String(char *a){

int i;

for( i = 0; a[i] != '\0'; i++)

Lcd_Write_Char(a[i]);}
void Lcd_Shift_Right(){

Lcd_Cmd(0x01);

Lcd_Cmd(0x0C);}

void Lcd_Shift_Left(){

Lcd_Cmd(0x01);

Lcd_Cmd(0x08);}

void ADC_Init(){

ADCON0bits.CHS0 = 0;

ADCON0bits.CHS1 = 0;

ADCON0bits.CHS2 = 0;

ADCON1bits.ADFM = 1;

ADCON1bits.PCFG0 = 0;

ADCON1bits.PCFG1 = 0;

ADCON1bits.PCFG2 = 0;

ADCON1bits.PCFG3 = 0;

ADCON0bits.GO_DONE = 1;

ADCON0bits.ADON = 1;}

unsigned int ADC_Read(unsigned char channel){

ADCON0 &= 0x11000101;

ADCON0 |= channel<<3;

__delay_ms(2);

ADCON0bits.GO_nDONE = 1;

while(ADCON0bits.GO_nDONE);

return((ADRESH<<8)+ADRESL);}

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

#define _XTAL_FREQ 20000000

#define Baud_rate 9600

#define RS RD2

#define EN RD3

#define D4 RD4

#define D5 RD5
#define D6 RD6

#define D7 RD7

// 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 = OFF // Brown-out Reset Enable bit (BOR enabled)

#pragma config LVP = ON // 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 <stdio.h>

#include <stdlib.h>

#include "lcd.h"

void UART_Init(){

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 a){

while(!TXIF);

TXREG = a;}
char UART_get_char(){

if(OERR){

CREN = 0;

CREN = 1; }

while(!RCIF);

return RCREG;}

void putch(char a){

UART_send_char(a);}

void menuList(){

printf("\r Dobrodosli! Odaberite opciju 1, 2 ili 3: ");

printf("\r 1) Upali svaku drugu diodu (ukupno 10 dioda) ");

printf("\r 2) Pokreni brojac na 7seg displeju da broji od 3-8");

printf("\r 3) Trenutna temperatura(koja koristi LM35 senzor da iscita vrijednost i ispise na displeju)\r");}

void main(void){

TRISD = 0;//Lcd

TRISB = 0;//7seg

TRISC = 0;//led,virtualni

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

char broj = 3;

int i=0;

UART_Init();

ADC_Init();

Lcd_Init();

Lcd_Clear();

char get_value;

while(1){

menuList();

get_value=UART_get_char();

printf("\r Odabrali ste opciju: ");

UART_send_char(get_value);
switch(get_value){

case '1':

RC1 = 1;

RC2 = 0;

__delay_ms(1000);

RC2 = 1;

RC1 = 0;

__delay_ms(1000);

Lcd_Clear();

Lcd_Set_Cursor(1,1);

Lcd_Write_String("Odabrana je opcija 1.");

Lcd_Set_Cursor(2,1);

__delay_ms(2000);

break;

case '2':

printf("\r Opcija 2");

Lcd_Clear();

Lcd_Set_Cursor(1,1);

Lcd_Write_String("Opcija 2");

/*

for(int j=3;j<9;j++){

PORTB=DISPLAY[j];

__delay_ms(250);

*/

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

PORTB = Display[i];

__delay_ms(250);}
__delay_ms(2000);

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

PORTB = Display[i];

__delay_ms(250);

__delay_ms(2000);

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

if( i%2 == 0){

PORTB = Display[i];

__delay_ms(250);

for (int i=1 ; i<= 9; i++){

if(i %2 != 0){

PORTB = Display[i];

__delay_ms(250); } }

PORTB = Display[0];

__delay_ms(2000);

break;

case '3':

printf("\r Opcija 3");

float temp;

int volt, temp1, temp2;

int c1,c2,c3,c4;

temp=(ADC_Read(0));

volt = temp * 4.88281;

temp2 = volt * 10.0;

c1 = (temp2 / 1000) % 10;


c2 = (temp2 / 100) % 10;

c3 = (temp2 / 10) % 10;

c4 = (temp2 / 1) % 10;

Lcd_Clear();

Lcd_Set_Cursor(1,1);

Lcd_Write_String("TEMPERATURA JE");

Lcd_Set_Cursor(2,1);

Lcd_Write_Char(temp +'0');

/* Lcd_Write_Char(c2 +'0');

Lcd_Write_String(".");

Lcd_Write_Char(c3 +'0');

Lcd_Write_Char(c4 +'0');*/

__delay_ms(1000);

break;

}}

#pragma config FOSC = HS

#pragma config WDTE = OFF

#pragma config PWRTE = OFF


#pragma config BOREN = OFF

#pragma config LVP = ON

#pragma config CPD = OFF

#pragma config WRT = OFF

#pragma config CP = OFF

#include <xc.h>

#define _XTAL_FREQ 8000000

void main(void){

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

TRISB = 0;

TRISC = 1;

char br = 0;

while(1){

if(!RC2){

if(!RC2){

PORTB = Display[br];

__delay_ms(250);

br++;

}if(br > 9){

br = 0;

}else {

PORTB = Display[br];}}}
// 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 = 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)

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

// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 20000000


#define RS RD2

#define EN RD3

#define D4 RD4

#define D5 RD5

#define D6 RD6

#define D7 RD7

#include "lcd.h"

void main(void){

TRISD = 0;

Lcd_Init();

Lcd_Clear();

while(1){

Lcd_Set_Cursor(1,1);

Lcd_Write_String("ARNELA");

__delay_ms(200);

}
#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)

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

// Use project enums instead of #define for ON and OFF.

#include <xc.h>

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

#define _XTAL_FREQ 20000000

#define s1 RC0

#define s2 RC1

#define s3 RC2

#define s4 RC3

#define CRVENA RD0

#define ZELENA RD1

void main(void){

PORTB = 0;

TRISB = 0;

PORTC = 0;

TRISC = 0;

PORTD = 0;

TRISD = 0;

char DISPLAY[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

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

for(int j=0;j<6;j++){

for(int k=0;k<6;k++){

for(int z=0;z<10;z++){

for(int a=0;a<10;a++){
if(i==0 && j==0 && k==5 && z==9 ){

CRVENA = 1;

ZELENA = 0;

else if(i==0 && j==1 && k==5 && z==9){

CRVENA = 1;

ZELENA = 0; }

s2=1;

s3=1;

s4=1;

PORTB=DISPLAY[z];

s1=0;

__delay_ms(5);

s1=1;

s3=1;

s4=1;

PORTB=DISPLAY[k];

s2=0;

__delay_ms(5);

s1=1;

s2=1;

s4=1;

PORTB=DISPLAY[j];

s3=0;

__delay_ms(5);

s1=1;
s2=1;

s3=1;

PORTB=DISPLAY[i];

s4=0;

__delay_ms(5);

}}}}}}

BROJANJE OD 0 -9

#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)

#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#define _XTAL_FREQ 20000000

#define Baud_rate 9600

unsigned char const DISPLAY[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

void UART_init(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 a){

while(!TXIF);

TXREG = a; }

char UART_get_char(){

if(OERR){

CREN = 0;

CREN = 1;}

while(!RCIF);

return RCREG;}

void putch(char a){

UART_send_char(a);}

void showList(){

printf("\r");

printf("1. Brojanje od 0 do 9 \r");

printf("2. Brojanje od 9 do 0 \r");}


void main(void) {

TRISB=0;

UART_init();

char get_value;

int vrijednost;

char broj1=0;

char broj2=9;

do {

showList();

get_value=UART_get_char();

UART_send_char(get_value);

vrijednost = get_value - '0';

if(vrijednost==1){

while(1){

PORTB=(DISPLAY[broj1]);

__delay_ms(250);

broj1++;

if(broj1>9){

break; }}}

else if(vrijednost==2){

while(1){

PORTB=DISPLAY[broj2];

__delay_ms(250);

broj2--;

if(broj2==0){

break; }}}

else{ printf("\r Unijeli ste pogresnu vrijednost!"); }}

while(get_value!=3);}
VIRTUAL SABIRANJE

#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)

#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#define _XTAL_FREQ 20000000

#define Baud_rate 9600

void main(void) {

TRISB = 0x00;

RB0 = 0;

RB1 = 0;

UART_init();

char get_value;

char get_value_1;

char get_value_2;

int value_1;

int value_2;

int rezultat;

char rezultat_krajnji;

do{
printf("\r Odaberite jedan broj:\r");

get_value_1=UART_get_char();

UART_send_char(get_value_1);

value_1 = get_value_1 - '0';

printf("\r Odaberite drugi broj:\r");

get_value_2=UART_get_char();

UART_send_char(get_value_2);

value_2 = get_value_2 - '0';

printf("\r Odaberite opciju: ");

printf("\r 1. Sabiranje");

printf("\r 2. Oduzimanje");

printf("\r 3. Mnozenje");

get_value=UART_get_char();

UART_send_char(get_value);

switch(get_value){

case '1':

rezultat = value_1+value_2;

printf("\r Rezultat je:\r");

rezultat_krajnji = rezultat + '0';

UART_send_char(rezultat_krajnji);

if(rezultat>5){

RB0 = 1;

RB1 = 0; }

else if(rezultat<5){

RB1 = 1;

RB0 = 0; }

break;

case '2':
rezultat = value_1-value_2;

printf("\r Rezultat je:\r");

rezultat_krajnji = rezultat + '0';

UART_send_char(rezultat_krajnji);

if(rezultat>5){

RB0 = 1;

RB1 = 0; }

else if(rezultat<5){

RB1 = 1;

RB0 = 0; }

break;

case '3':

rezultat = value_1*value_2;

printf("\r Rezultat je:\r");

rezultat_krajnji = rezultat + '0';

UART_send_char(rezultat_krajnji);

if(rezultat>5){

RB0 = 1;

RB1 = 0; }

else if(rezultat<5){

RB1 = 1;

RB0 = 0; }

break; }}

while(get_value!=4); }

You might also like