0% found this document useful (0 votes)
46 views3 pages

Comunicación SPI: Maestro

This document contains code for a microcontroller program that uses digital and analog input/output ports to read an analog sensor value and light LEDs in sequences based on the sensor reading. The code defines pin configurations, constants for LED patterns, and functions for analog to digital conversion, communication via serial peripheral interface (SPI), and delay timing. The main loop continuously reads the analog sensor, sends the value over SPI, lights different LED patterns based on the sensor value, and adds a delay before repeating.

Uploaded by

ErosHM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Comunicación SPI: Maestro

This document contains code for a microcontroller program that uses digital and analog input/output ports to read an analog sensor value and light LEDs in sequences based on the sensor reading. The code defines pin configurations, constants for LED patterns, and functions for analog to digital conversion, communication via serial peripheral interface (SPI), and delay timing. The main loop continuously reads the analog sensor, sends the value over SPI, lights different LED patterns based on the sensor value, and adds a delay before repeating.

Uploaded by

ErosHM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Eros Efrn Hiplito Mndez

Comunicacin SPI
Maestro
#define _LEGACY_HEADERS //Added for compiler versions 9.81+
#include <htc.h>
#include <stdio.h>
#define DS1_on 0b00000001;
#define DS2_on 0b00000011;
#define DS3_on 0b00000111;
#define DS4_on 0b00001111;
#define DS5_on 0b00011111;
#define DS6_on 0b00111111;
#define DS7_on 0b01111111;
#define DS8_on 0b11111111;
int advalue = 0;
int vdsw = 0;
void pause( unsigned short usvalue );

//Establish pause routine function

__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & DUNPROTECT &
BORDIS & IESODIS & FCMDIS & LVPDIS & DEBUGDIS);
__CONFIG(BORV40);
void
main(void)
{
ANSEL = 0;
ANSELH = 0;

// Set PORTA all Digital

TRISA = 0;
// All PORTA Outputs
TRISC = 0b00010000;
TRISB = 0b00000000; //INPUTS
TRISD= 0xFF;
PORTC=0;
PORTA=0b00000000;
PORTB=0b00000000;
PORTD=0b00000000;
SSPCON=0x10;
SSPCON2=0x00;
SSPSTAT=0xC0;
SSPEN=1;
while(1==1)
{
PORTA=0b00000001;
vdsw=PORTD;
SSPBUF=vdsw;
while (!BF);
advalue=SSPBUF;
PORTA=0b00010000;

if (advalue >30) //Test if A/D value is less than 60 decimal


{
PORTB = DS1_on;
//Less than 60, light DS1 LED
}
if (advalue >60) //Test if A/D value less than 120 decimal
{
PORTB = DS2_on;
//Less than 120, light DS1,2 LEDs
}
if (advalue >90) //Test if A/D value less than 180
{
PORTB = DS3_on;
//Less than 180, light DS1,2,3 LEDs
}
if (advalue > 120)
//Test if A/D value less than 240
{
PORTB = DS4_on;
//Less than 240, light DS1,2,3,4 LEDs
}
if (advalue >150)
{
PORTB = DS5_on;
}
if (advalue >180)
{
PORTB = DS6_on;
}

//Test if A/D value is less than 60 decimal

if (advalue >210)
{
PORTB = DS7_on;
}
if (advalue > 240)
{
PORTB = DS8_on;
}

//Test if A/D value less than 180

if (advalue <=30)
{
PORTB = 0;
}
pause(10);
}
}

//Less than 60, light DS1 LED


//Test if A/D value less than 120 decimal
//Less than 120, light DS1,2 LEDs

//Less than 180, light DS1,2,3 LEDs


//Test if A/D value less than 240
//Less than 240, light DS1,2,3,4 LEDs
//Test if A/D value equal to or greater than 240
//Set all LEDs off

void pause( unsigned short usvalue )


{
unsigned short x;
for (x=0; x<=usvalue; x++)
{
msecbase();
}
}
void msecbase(void)
{

//Loop through a delay equal to usvalue


// in milliseconds.
//Jump to millisec delay routine

OPTION = 0x01;
TMR0 = 0xD;
while(!T0IF);
T0IF = 0;

//Set prescaler to TMRO 1:4


//Preset TMRO to overflow on 250 counts
//Stay until TMRO overflow flag equals 1
//Clear the TMR0 overflow flag

Esclavo
#define _LEGACY_HEADERS //Added for compiler versions 9.81+
#include <htc.h>
#include <stdio.h>
int advalue = 0;
int vadc = 0;
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & DUNPROTECT &
BORDIS & IESODIS & FCMDIS & LVPDIS & DEBUGDIS);
__CONFIG(BORV40);
void
main(void)
{
ANSEL =0B11011111;//Puerto A analogico
ANSELH =0;//Puerto B digital
TRISB=0;
ADCON1=0B00110000;
ADCON0=0B00000001;
TRISA = 0b11111111;
TRISC = 0b00011000;
TRISD= 0X00;;
PORTA=0x00;
PORTB=0x00;
SSPCON=0X14;
SSPCON2=0X00;
SSPSTAT=0x40;
SSPEN=1;
ADON=1;
while(1==1)
{
while (!BF);
advalue=SSPBUF;
PORTD=advalue;
GODONE=1;
while(GODONE==1);
{
}
vadc=ADRESH;
SSPBUF=vadc;
}
//end while BF
}
//end while permanente
}
//end main

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy