trakomaster
Joined: 13 Nov 2011 Posts: 1 Location: Lima, Peru
|
MAX7219 example and use spi software 5 code lines so easy |
Posted: Sun Nov 13, 2011 12:57 am |
|
|
This is an easy program for my power supply, it's really simple, just look at #use spi and void maxsend(){} and that's all. I don't know why people write lots of lines, but this code works with 5 lines including curly braces.
12F683 has no spi hardware so this is useful.
I hope you guys like it. Send a message if you want the complete project with proteus.
I'm not good at writing in English. Please don't be mean.
Leave your comments
Code: | #include <12f683.h>
#fuses intrc_io,noput,nowdt,noprotect,nomclr,nobrownout,noieso,nofcmen
#device adc=10
#use delay(clock=4M)
#use fast_io(a)
#use spi(do=pin_a0,clk=pin_a2,bits=16,load_active=1,load=pin_a1,idle=1)
//We have to write this directive, you can add stream and change the pin do, clk, load
//I've used bits 16 'cause max7219 needs 16 bits =)
//The idle is 1, if you put 0, nothing will appear on your displays <- this is really important
int address, data, adqnum, digit[4]; //address, data max's variables
//adqnum for 100 samples
//digit[4] this array keeps 4 digits used in this project
int16 packetmax, result; //packetmax goes to the max7219
//result keeps the value used to obtain digits
int32 adcvalue=0; //this variable keep the adc value read 100 times
float calculus; //variable used for (adcvalue * (5/1023))
void maxsend(){ //routine for max7219 just
packetmax=make16(address,data); //make a 16 bit variable address byte high, data byte low
spi_xfer(packetmax); //transfer using spi software
} //just 5 lines including #use spi. It's really simple right?
void maxsetup(){ //configure properly
address=0x09, data=0xff, maxsend(); //it's up to you
address=0x0a, data=0x01, maxsend(); //just read a minute the datasheet
address=0x0b, data=0x03, maxsend(); //it's easy and fast.
address=0x0f, data=0x00, maxsend();
address=0x0c, data=0x01, maxsend();
}
void algebra_digits(){
calculus=adcvalue*0.4887585533; //500 is the max value, 500=5.00V, but it's float
result=calculus; //transfer the correct value to int result
//if you want to measure a higher voltage just change the float value
//and don't forget using resistors as needed.
//the following 4 lines are used to obtain digits
digit[0]=result-((result/10)*10); //units place
digit[1]=(result/10)-((result/100)*10); //tens place
digit[2]=(result/100)-((result/1000)*100); //hundreds place
digit[3]=result/1000; //thousands place
}
void main(){
setup_comparator(nc_nc_nc_nc); //no comparator module
setup_adc(adc_clock_div_16); //Tad = 4.0us
setup_adc_ports(an3_analog); //gpio4 as analog input, u can choose your favorite pin
set_adc_channel(3); //select channel 3 =gpio4
set_tris_a(0b00011000); //set port
delay_ms(1000); //wait just for relaxing =), this line is not that important
maxsetup(); //
while(1){
for(adqnum=1;adqnum<=100;adqnum++){ //collect 100 samples, I usually do this, u can use your favorite method
adcvalue=adcvalue+read_adc(); //for acquisition.
delay_us(20);
}
adcvalue=adcvalue/100;
algebra_digits(); //call algebra routine to get each digit
address=0x01, data=digit[0], maxsend();
address=0x02, data=digit[1], maxsend();
address=0x03, data=(digit[2] | 0b10000000), maxsend(); //use OR for adding decimal point
address=0x04, data=digit[3], maxsend();
adcvalue=0; //clean adcvalue
}
} | _________________ Physics works!!! |
|