/*
OUTBYTE.C MPB 2-1-07 V1.0
*/
#include " 16F877A.h " // MCU select
void main( ) // Main block
{
output_D(255); // Switch on outputs
}
.
/*
Source code file: VARI.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs an 8-bit variable
Simulation circuit: OUTBYTE.DSN
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
#include " 16F877A.h "
void main()
{
int x; // Declare variable and type
x = 99; // Assign variable value
output_D(x) ; // Display the value in binary
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// Source code file: ENDLESS.C
// Program function: Outputs variable count
#include " 16F877A.h "
void main()
{
int x; // Declare variable
while(1) // Loop endlessly
{ output_D(x); // Display value
x ++ ; // Increment value
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// IFIN.C Tests an input
#include " 16F877A.h "
void main()
{
int x; // Declare variable
output_D(0); // Clear all outputs
while(1) // Loop always
{
x = input(PIN_C0); // Get input state
if(x = = 1)output_high(PIN_D0); // Change output
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// WHILOOP.C Input switch controls output flashing
#include " 16F877A.h "
#use delay (clock = 1000000) // MCU clock = 1 MHz
void main()
{
while(1)
{
while(input(PIN_C0)) // Repeat while switch open
{
output_high(PIN_D0);
delay_ms(300); // Delay 0.3s
output_low(PIN_D0);
delay_ms(500); // Delay 0.5s
}
output_low(PIN_D0); // Switch off LED
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// FORLOOP.C Repeat loop a set number of times
#include " 16F877A.h "
#use delay (clock = 1000000)
void main()
{
int x;
while(input(PIN_C0)) { } ; // Wait until switch closed
for (x = 0; x < 5; x ++) // For loop conditions
{
output_high(PIN_D0); // Flash sequence
delay_ms(500);
output_low(PIN_D0);
delay_ms(500);
}
while(1); // Wait for reset
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
#include " 16F877A.h "
#use delay (clock = 1000000)
void main()
{
int step;
while(1) // Keep checking switch
{
while(!input(PIN_C0)) // Siren while switch ON
{
for(step = 0;step < 255;step ++ ) // Loop control
{
output_high(PIN_D0); // Sound sequence
delay_us(step);
output_low(PIN_D0);
delay_us(step);
}
}
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// DOWHILE.C
// Comparison of WHILE and DO WHILE loops
# include " 16F877A.H "
main()
{
int outbyte1 = 0;
int outbyte2 = 0;
int count;
// This loop is not executed ..............
count = 0;
while (count! = 0)
{ output_C(outbyte1);
outbyte1 ++ ;
count -- ;
}
// This loop is executed...................
count = 0;
do
{ output_C(outbyte2);
outbyte2 ++ ;
count-- ;
} while (count! = 0);
while(1) { } ;
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// CONTINUE.C
// Continue, break, and goto jumps
# include " 16F877A.H"
# use delay(clock = 4000000)
main()
{
int outbyte;
again: outbyte = 0; // Destination of goto
while(1)
{
output_C(outbyte); // Foreground operation
delay_ms(10);
outbyte ++ ; // Increments Port C
if (!input(PIN_D0)) continue; // Skip other tests if input 0
low
if (!input(PIN_D1)) break; // Terminate loop if input 1 low
delay_ms(100); // Debounce inputs
if (outbyte == 100) goto again; // Restart at 100
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// SWITCH.C
// Switch and if..else sequence control
// Same result from both sequences
///////////////////////////////////////////////////////////////
# include " 16F877A.h "
void main()
{
int8 inbits;
while(1)
{
inbits = input_D(); // Read input byte
switch(inbits) // Test input byte
{
case 1: output_C(1); // Input = 0 x 01, output = 0 x 01
break; // Quit block
case 2: output_C(3); // Input = 0 x 02, output = 0 x 03
break; // Quit block
case 3: output_C(7); // Input = 0 x 03, output = 0 x 07
break; // Quit block
default:output_C(0); // If none, output = 0 x 00
}
if (input(PIN_D0)) output_C(1); // This block has same effect
if (input(PIN_D1)) output_C(2);
if (input(PIN_D0) && input(PIN_D1)) output_C(7);
else output_C(0);
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// FUNC1.C
// Function call and program structure
///////////////////////////////////////////////////////////////
# include " 16F877A.H "
int8 outbyte = 1; // Declare global variables
int16 n;
void out() ////////////////////////// Start of function block
{
while (outbyte! = 0) // Start loop, quit when output 0
{ output_C(outbyte); // Output code 1 - 0xFF
outbyte ++ ; // Increment output
for(n = 1;n < 500;n++ ); // Delay so output is visible
}
}
main() ////////////////////////// Start of main block
{
out(); // Function call
while(1); // Wait until reset
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// FUNC2.C
// Uses global variables only
///////////////////////////////////////////////////////////////
# include " 16F877A.H "
int8 outbyte = 1; // Declare global variables
int16 n,count;
void out() //////////////////// Function to run output count
{
while(outbyte! = 0)
{ output_C(outbyte);
outbyte ++ ;
for(n = 1;n < count;n++); // Use global value for count
}
}
main() //////////////////// Main block
{
count = 2000; // Set variable value
out(); // Call function
while(1); // Wait for reset
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// FUNC3.C
// Uses local variables
///////////////////////////////////////////////////////////////
# include " 16F877A.H "
int8 outbyte = 1; // Declare global variables
int16 count;
int out(int16 t) ////////////// Declare argument types
{
int16 n; // Declare local variable
while (input(PIN_D0)) // Run at speed t
{ outbyte ++ ;
for(n = 1;n < t;n++ );
}
return outbyte; // Return output when loop stops
}
main() ////////////////////////////////////////////
{
count = 50000;
out(count); // Pass count value to function
output_C(outbyte); // Display returned value
while(1);
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// LCD.C
// Serial LCD test - send character using putc() and printf()
///////////////////////////////////////////////////////////////
# include " 16F877A.h "
# use delay(clock = 4000000)
#use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1) // Define speed
and pins
void main()
{
char acap = ' A ' ; // Test data
delay_ms(1000); // Wait for LCD to wake up
putc(254); putc(1); // Home cursor
delay_ms(10); // Wait for LCD to finish
while(1)
{
putc(acap); // Send test character
putc(254); putc(192); delay_ms(10); // Move to second row
printf( " ASCII %c CHAR %d " ,acap,acap); // Send test data again
while(1);
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
/* FLOAT.C MPB 4 _ 3 _ 07
Displays variable types and ranges
****************************************************/
# include " 16F877A.h"
# use delay(clock = 4000000)
# use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1)
int1 minbit = 0, maxbit = 1;
signed int8 minbyte =- 127, maxbyte = 127;
signed int16 minword =- 32767, maxword = 32767;
signed int32 minlong =- 2147483647, maxlong = 2147483647;
float testnum = 12345.6789;
void main()
{
delay_ms(1000); // Wait for LCD to wake
putc(254); putc(1); // Home cursor
delay_ms(10); // Wait for LCD to do
while(1)
{
printf( " Bit:%d or %d " ,minbit, maxbit); delay_ms(1000);
putc(254); putc(1); delay_ms(10);
printf( " Byte %d to %d " ,minbyte, maxbyte); delay_ms(1000);
putc(254); putc(1); delay_ms(10);
printf( " Word %Ld " ,minword); putc(254); putc(192);
delay_ms(10); printf(" to %Ld " ,maxword); delay_ms(1000);
putc(254); putc(1); delay_ms(10);
printf(" Long %Ld " ,minlong); putc(254); putc(192);
delay_ms(10); printf( " to %Ld" ,maxlong); delay_ms(1000);
putc(254); putc(1); delay_ms(10);
printf( " Float %5.4g " ,testnum); putc(254); putc(192);
delay_ms(10); printf(" or %e " , testnum); delay_ms(1000);
putc(254); putc(1); delay_ms(10);
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
/*
Source Code Filename: CALC.C
Author/Date/Version: MPB 21 - 1 2- 07
Program Description: Calculator demo program
Hardware/simulation : CALC.DSN
**********************************************************************/
#include " 16F877A.h "
#use delay(clock= 4000000)
#use rs232(baud = 9600,xmit = PIN_D7,rcv= PIN_D0)
// Declare variables **************************************************
int akey, keynum, opcode, numofdigs, start;
int32 num1, num2, result, rem1, rem2, rem3, rem4;
int32 hunsdig, tensdig, onesdig;
int32 hunthous, tenthous, thous, hunds, tens, ones;
// Declare functions **************************************************
void scankey(); // Read keypad
void makenum(); // Construct input decimal from keys
// MAIN PROGRAM: Get numbers & calculate ********************************
void main()
{
for(;;)
{
// Get numbers ...................................................
delay_ms(500); putc(254); putc(1); delay_ms(10); // Clear display
numofdigs = onesdig = tensdig = hunsdig = 0; akey = 0 x 30;
do
{ scankey(); // Get first number
putc(akey);
if((akey > = 0x 30) && (akey < = 0x 39)) makenum();
} while((akey >= 0 x 30) && (akey <= 0 x 39));
num1 = (onesdig + (tensdig*10) + (hunsdig*100)); // Calculate it
opcode = akey;
numofdigs = onesdig = tensdig = hunsdig = 0; akey = 0 x 30; // Get second number
do
{ scankey();
putc(akey);
if((akey >= 0 x 30)&& (akey <= 0 x 39)) makenum();
} while((akey<= 0x 30)&&(akey <= 0 x 39));
num2 = (onesdig + (tensdig*10) + (hunsdig*100)); // Calculate it
// Calculate result.............................................
if(opcode = = 0 x 2F) result = num1/num2;
if(opcode = = 0 x 2 A) result = num1*num2;
if(opcode = = 0 x 2D) result = num1 - num2;
if(opcode= = 0 x 2B) result = num1 + num2;
//Calc result digits............................................
hunthous = result/100000; rem1 = result - (hunthous*100000);
tenthous = rem1/10000; rem2 = rem1 - (tenthous*10000);
thous = rem2/1000; rem3 = rem2 - (thous*1000);
hunds = rem3/100; rem4 = rem3 - (hunds*100);
tens = rem4/10; ones = rem4 - (tens*10);
// Display digits...............................................
start = 0;
if(hunthous! = 0) { putc(hunthous + 0 x 30);start = 1; }
if((tenthous! = 0)||(start = = 1)) { putc(tenthous + 0 x 30); start = 1; }
if((thous! = 0) || (start = = 1)) { putc(thous + 0 x 30); start = 1; }
if((hunds! = 0) || (start = = 1)) { putc(hunds + 0 x 30); start = 1; }
if((tens! = 0) || (start = = 1)) { putc(tens + 0 x 30); start = 1; }
if((ones! = 0) || (start = = 1)) { putc(ones + 0 x 30); start = 1; }
while(akey! = 0xFF) scankey();
}
}
// PROCEDURE: Derive input digits *********************************
void makenum()
{
keynum = akey - 0 x 30;
numofdigs + + ;
if(numofdigs = = 3)
{ hunsdig = tensdig; tensdig = onesdig; onesdig = keynum; }
if(numofdigs = = 2)
{ tensdig = onesdig; onesdig = keynum; }
if(numofdigs = = 1)
onesdig = keynum;
}
// PROCEDURE: Scans keypad attached to Port D *********************
void scankey()
{
akey = 0;
while(akey = =0)
{
output_b(255); output_low(PIN_B1);
if(!input(PIN_D1))
{ akey = 0 x 37; delay_ms(50); while(!input(PIN_D1)) { } ; }
if(!input(PIN_D2))
{ akey = 0 x 38; delay_ms(50); while(!input(PIN_D2)) { } ; }
if(!input(PIN_D3))
{ akey = 0 x 39; delay_ms(50); while(!input(PIN_D3)) { } ; }
if(!input(PIN_D4))
{ akey = 0 x 2F; delay_ms(50); while(!input(PIN_D4)) { } ; }
output_b(255); output_low(PIN_B2);
if(!input(PIN_D1))
{ akey = 0 x 34; delay_ms(50); while(!input(PIN_D1)) { } ; }
if(!input(PIN_D2))
{ akey = 0 x 35; delay_ms(50); while(!input(PIN_D2)) { } ; }
if(!input(PIN_D3))
{ akey = 0 x 36; delay_ms(50); while(!input(PIN_D3)) { } ; }
if(!input(PIN_D4))
{ akey = 0 x 2A; delay_ms(50); while(!input(PIN_D4)) { } ; }
output_b(255); output_low(PIN_B4);
if(!input(PIN_D1))
{ akey = 0 x 31; delay_ms(50); while(!input(PIN_D1)) { } ; }
if(!input(PIN_D2))
{ akey = 0 x 32; delay_ms(50); while(!input(PIN_D2)) { } ; }
if(!input(PIN_D3))
{ akey = 0 x 33; delay_ms(50); while(!input(PIN_D3)) { } ; }
if(!input(PIN_D4))
{ akey = 0 x 2D; delay_ms(50); while(!input(PIN_D4)) { } ; }
output_b(255); output_low(PIN_B5);
if(!input(PIN_D1))
{ akey = 0xFF; putc(254); putc(1); delay_ms(500); }
if(!input(PIN_D2))
{ akey = 0 x 30; delay_ms(50); while(!input(PIN_D2)) { } ; }
if(!input(PIN_D3))
{ akey = 0 x 3D; delay_ms(50); while(!input(PIN_D3)) { } ; }
if(!input(PIN_D4))
{ akey = 0 x 2B; delay_ms(50); while(!input(PIN_D4)) { } ; }
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// ARRAYS.C
// Demo of numerical and string arrays
// Attach ARRAYS.COF to LCD.DSN to display
///////////////////////////////////////////////////////////
# include " 16F877A.h"
# use delay(clock=4000000)
# use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1)
main()
{
int8 aval = 0, n; // Declare single variables
int8 anum[10]; // Declare integer array
char astring[16]; // Declare character array
// Start LCD...............................................
delay_ms(1000);
putc(254); putc(1); delay_ms(10);
// Assign data to arrays...................................
for ( n = 0; n < 10; n + + ) { anum[n] = aval; aval + + ; }
strcpy(astring, " Hello! " );
// Display data.............................................
for ( n = 0; n < 10; n + + ) printf( " %d " ,anum[n]);
putc(254); putc(192); delay_ms(10);
puts(astring);
while(1); // Wait
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
/* ANALIN.C MPB 5-1-07
Read & display analog input
***************************************************************/
#include " 16F877A.h "
#device ADC = 8 //8-bit conversion
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1) //LCD output
void main() //*************************************************
{
int vin0; // Input variable
setup_adc(ADC_CLOCK_INTERNAL); // ADC clock
setup_adc_ports(ALL_ANALOG); // Input combination
set_adc_channel(0); // Select RA0
for(;;)
{ delay_ms(500);
vin0 = read_adc(); //Get input byte
vin0 = (vin0/32) + 0 x 30; //Convert to ASCII
putc(254); putc(1); delay_ms(10); // Clear screen
printf( " Input = " ); putc(vin0); // Display input
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
/* VOLTS.C MPB 25-3-07
Read & display 10-bit input voltage
*****************************************************************/
#include " 16F877A.h "
#device ADC = 10 // 10-bit operation
#use delay(clock = 4000000)
#use rs232(baud = 9600,xmit= PIN_D0,rcv=PIN_D1)
void main() //**************************************************
{
int chan;
float analin[8], disvolts[8]; // Array variables
setup_adc(ADC_CLOCK_INTERNAL); // ADC Clock source
setup_adc_ports(AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF); // ADC inputs
while(1) // Loop always
{
for(chan = 0;chan < 8;chan + + ) // Read 8 inputs
{ delay_ms(1000); // Wait 1 sec
set_adc_channel(chan); // Select channel
analin[chan]= read_adc(); // Get input
disvolts[chan]=(analin[chan])/400; // Scale input
putc(254);putc(1);delay_ms(10); // Clear display
printf( " RA%d = %4.3 g " ,chan,disvolts[chan]); // Display volts
}
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// INTEXT.C MPB 10-4-07
// Demo external interrupt RB0 low interrupts foreground output count
#include " 16F877A.h "
#use delay(clock = 4000000)
#int_ext // Interrupt name
void isrext() // Interrupt service routine
{ output_D(255); // ISR action
delay_ms(1000);
}
void main() //********************************************
{
int x;
enable_interrupts(int_ext); // Enable named interrupt
enable_interrupts(global); // Enable all interrupts
ext_int_edge(H_TO_L); // Interrupt signal polarity
while(1) // Foreground loop
{
output_D(x); x + + ;
delay_ms(100);
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// PWM.C MPB 11-4-07
// Demo PWM output, MCU clock = 4 MHz
#include " 16F877A.h "
void main()
{
setup_ccp1(ccp_pwm); // Select timer and mode
set_pwm1_duty(500); // Set on time
setup_timer_2(T2_DIV_BY_16,248,1); // Clock rate & output period
while(1) { } // Wait until reset
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// PERIOD.C MPB 11-4-07
// Demo of period measurement
#include " 16F877A.h " //****************************
#int_ccp1 // Interrupt name
void isr_ccp1() // Interrupt function
{
set_timer1(0); // Clear Timer1
clear_interrupt(INT_CCP1); // Clear interrupt flag
}
void main() //************************************
{
setup_timer_1(T1_INTERNAL); // Internal clock
setup_ccp1(CCP_CAPTURE_RE); // Capture rising edge on RC2
enable_interrupts(GLOBAL); // Enable all interrupts
enable_interrupts(INT_CCP1); // Enable CCP1 interrupt
while(1) { }
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// HARDRS232.C MPB 13-6-07
// Serial I/O using hardware RS232 port
#include " 16F877A.h "
#use delay(clock= 8000000) // Delay function needed for RS232
#use rs232(UART1) // Select hardware UART
void main() //************************************
{
int incode;
setup_uart(9600); // Set baud rate
while(1)
{ incode = getc(); // Read character from UART
printf( " ASCII = %d " ,incode); // Display it on
putc(13); // New line on display
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// SPITRANSMIT.C MPB 20-6-07
// Serial I/O using SPI synchronous link
// Simulation hardware SPIC.DSN, transmitter program attached to U2
#include " 16F877A.h "
void main() //*****************************************
{
int sendnum;
setup_spi(spi_slave); // Set SPI slave mode
while(1)
{ sendnum = input_D(); // Get BCD input
spi_write(sendnum); // Send BCD code to master
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// SPIMASTER.C MPB 20-6-07
// Serial I/O using SPI synchronous link
// Simulation hardware SPIC.DSN, master program, attach to U1
#include " 16F877A.h "
void main() //***************************************************
{
int number;
setup_spi(spi_master); // Set SPI master mode
while(1)
{ number _ spi_read(); // Read SPI input BCD code
spi_write(number); // Resend BCD code to slave
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// SPIRECEIVE.C MPB 20-6-07
// Serial I/O using SPI synchronous link
// Simulation hardware SPI.DSN, receiver program, attach to U3
#include " 16F877A.h "
void main() //***************************************
{
int recnum;
setup_spi(spi_slave); // Set SPI slave mode
while(1)
{ recnum = spi_read(); // Read BCD code at SPI port
output_D(recnum); // Display it
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// PSPMASTER.C
// Test system master controller program, design file PSP.DSN, U1
#include " 16F877A.h "
void main() //**************************************
{
int sendbyte;
port_b_pullups(1); // Activate Port B pull-ups
while(1)
{ sendbyte = input_B(); // Get test byte
output_D(sendbyte); // Output on PSP bus
output_low(PIN_E2); // Select PSP slave
output_low(PIN_E1); // Write byte to slave port
output_high(PIN_E1); // Reset write enable
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// PSPSLAVE.C
// Test system slave controller program, design file PSP.DSN, U2
#include " 16F877A.h "
void main() //****************************************
{
int recbyte;
setup_psp(PSP_ENABLED); // Enable PSP slave port
while(1)
{ if(psp_input_full()) // If data have been received
{ recbyte = input_D(); // Copy in test data
output_C(recbyte); // Display data on bar graph
}
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 3.12 EEPROM Test Program
// EEPROM.C
// Internal data EEPROM test, design file EEPROM.DSN
#include " 16F877A.h "
#use delay(clock = 4000000)
void main() //////////////////////////////////////////////////////////
{
int writebyte, readbyte;
int maxadd, address;
port_b_pullups(1); // Enable Port B internal pull-ups
if(!input(PIN_C1)) // Write memory sequence //////////////////
{
address = 0; // First address
do
{ while(input(PIN_C0)) { } ; // Wait for button
writebyte = input_B(); // Get switch bank data
write_eeprom(address,writebyte); // Write data to EEPROM
readbyte = read_eeprom(address); // Read it back
output_D(readbyte); // Display data on bar graph
while(!input(PIN_C0)) { } ; // Wait for button release
address + + ; // Next EEPROM address
} while(writebyte! = 0); // Continue until data = 00
}
else // Read memory sequence ///////////////////
{
address = 0; // First address
do
{ while(input(PIN_C0)) { } ; // Wait for button
readbyte = read_eeprom(address); // Read data
output_D(readbyte); // Display it on bar graph
while(!input(PIN_C0)) { } ; // Wait for button release
address + + ; // Next address
} while(readbyte! = 0); // Continue until data = 00
while(1); // Done *************************************
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 3.13 Waveform Generator Source Code
// DACWAVE.C MPB 5-7-07
// Outputs waveforms to DAC, simulation file DAC.DSN
#include " 16F877A.H "
#include " MATH.H "
#use delay(clock = 20000000)
#use fast_io(D) // High speed output functions
int n, time = 10;
float step, sinangle;
float stepangle = 0.0174533; // 1 degree in radians
int amp[91]; // Output instant voltage array
// ISR to read push buttons ******************************************
#int_rb
void change()
{
if(time! = 255)
{ if (!input(PIN_B4)) time ++ ; } // Increase period
while(!input(PIN_B4));
if(time! = 0)
{ if (!input(PIN_B5)) time--; } // Decrease period
while(!input(PIN_B5));
if(!input(PIN_B6))reset_cpu(); // Restart program
if(!input(PIN_B7))for(n= 0;n < 91;n ++ )amp[n]= 0; // Zero output
}
void setwave() // Arbitrary waveform values **********************
{
amp[0] = 00;amp[1] = 00;amp[2] = 00;amp[3] = 00;amp[4] = 00;
amp[5] = 00;amp[6] = 00;amp[7] = 00;amp[8] = 00;amp[9] = 00;
amp[10]= 10;amp[11] = 00;amp[12] = 00;amp[13]= 00;amp[14] = 00;
amp[15]= 00;amp[16] = 00;amp[17]= 00;amp[18]= 00;amp[19] = 00;
amp[20]=20;amp[21]= 00;amp[22]= 00;amp[23] = 00;amp[24] = 00;
amp[25] = 00;amp[26] = 00;amp[27]= 00;amp[28] = 00;amp[29] = 00;
amp[30] = 30;amp[31] = 00;amp[32]= 00;amp[33] = 00;amp[34]= 00;
amp[35]= 00;amp[36] = 00;amp[37] = 00;amp[38] = 00;amp[39] = 00;
amp[40]=40;amp[41] = 00;amp[42]= 00;amp[43] = 00;amp[44] = 00;
amp[45]= 00;amp[46] = 00;amp[47]= 00;amp[48] = 00;amp[49] = 00;
amp[50]= 50;amp[51]= 00;amp[52]= 00;amp[53] = 00;amp[54] = 00;
amp[55] = 00;amp[56] = 00;amp[57]= 00;amp[58] = 00;amp[59]= 00;
amp[60] = 60;amp[61] = 00;amp[62] = 00;amp[63] = 00;amp[64] = 00;
amp[65] = 00;amp[66]= 00;amp[67] = 00;amp[68]= 00;amp[69]= 00;
amp[70] = 70;amp[71]= 00;amp[72] = 00;amp[73]= 00;amp[74] = 00;
amp[75] = 00;amp[76] = 00;amp[77]= 00;amp[78] = 00;amp[79]= 00;
amp[80]= 80;amp[81] = 00;amp[82]= 00;amp[83] = 00;amp[84] = 00;
amp[85]= 00;amp[86]= 00;amp[87]= 00;amp[88] = 00;amp[89] = 00;
amp[90] = 90;
}
void main() //*************************************************
{
enable_interrupts(int_rb); // Port B interrupt for buttons
enable_interrupts(global);
ext_int_edge(H_TO_L);
port_b_pullups(1);
set_tris_D(0);
// Calculate waveform values ***********************************
step = 0;
for(n = 0;n < 91;n ++ )
{
if(!input(PIN_B0)) amp[n] = 100; // Square wave offset
if(!input(PIN_B1)) // Calculate sine values
{ sinangle = sin(step*stepangle);
amp[n] = floor(sinangle*100);
step = step+1;
}
if(!input(PIN_B2)) amp[n] = n; // Triangular wave
if(!input(PIN_B3)) setwave(); // Arbitrary wave
}
// Output waveform vales ***************************************
while(1)
{ for(n = 0;n < 91;n ++ ) { output_D(100 + amp[n]); delay_us(time); }
for(n = 89;n > 0;n--) { output_D(100 + amp[n]); delay_us(time); }
for(n = 0;n < 91;n ++ ) { output_D(100-amp[n]); delay_us(time); }
for(n = 89;n > 0;n--) { output_D(100-amp[n]); delay_us(time); }
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
//MOTOR1.C MPB 17-4-07 PICDEM board test program
//Control motor from switch. Connect SW2-RA4, RD7-P1, RD4-N2
#include"16F917.h"
void main()
{
while(1)
{
if(!input(PIN_A4)) //Test switch
output_D(0x90); //Switch on motor
else output_D(0x00); //Switch off motor
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 4.8 Stepper Motor Test Program
// STEPTEST.C
// Test program for PICDEM Mechatronics Board stepper motor,
// basic full step mode. Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4
// plus all 6 jumpers for full bridge mode
// Motor moves 48 steps per rev (7.5 deg/step)
///////////////////////////////////////////////////
#include "16F917.h"
#use delay(clock=8000000)
void main()
{
while(1) //Loop always
{
output_D(0x80); //Switch on Drive 1
delay_ms(200);
output_D(0x10); //Switch on Drive 4
delay_ms(200);
output_D(0x40); //Switch on Drive 2
delay_ms(200);
output_D(0x20); //Switch on Drive 3
delay_ms(200);
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 4.10 Stepper Motor Speed Control Program
///////////////////////////////////////////////////////////////////////
// STEPSPEED.CMPB 22-4-07
// Program for PICDEM Mechatronics Board stepper motor, full step mode
// Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4 plus all 6 jumpers for full
// bridge mode plus SW3-RA3 and SW4-RA4. Motor speed SW3 up SW4 down
///////////////////////////////////////////////////////////////////////
#include "16F917.h"
#use delay(clock=8000000)
void main()
{
int8 time=16; // Variable step delay
while(1) //Loop always
{
//CHECK SWITCHES
if(!input(PIN_A3)) //Poll SW3
{ delay_ms(10); //Debounce
if(time!=1)time=time/2; //Not if min
}
while(!input(PIN_A3)){}; //Wait switch
if(!input(PIN_A4)) //Poll SW3
{ delay_ms(10); //Debounce
if(time!=128)time=time*2; //Not if max
}
while(!input(PIN_A4)){}; //Wait switch
//4 STEPS CLOCKWISE
output_D(0x20); delay_ms(time); //Step 1
output_D(0x40); delay_ms(time); //Step 2
output_D(0x10); delay_ms(time); //Step 3
output_D(0x80); delay_ms(time); //Step 4
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 4.11 Stepper Motor Speed and Direction Control
///////////////////////////////////////////////////////////////////////
// STEPDIR.C PICDEM Mechatronics Board stepper motor speed and dirc.
// Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4 plus all 6 jumpers(full bridge)
// SW2-RA2, SW3-RA3, SW4-RA4. Motor speed SW3 up SW4 down, motor dirc SW2
///////////////////////////////////////////////////////////////////////
#include "16F917.h" //MCU select
#use delay(clock=8000000) //Internal clock
int8 time=16; //Default speed
//PROCEDURES////////////////////////////////////////////////////
void speed() //Halve or double speed //////////
{
if(!input(PIN_A3)) //Poll SW3
{ delay_ms(10); //Debounce
if(time!=1)time=time/2; //Not if min
}
while(!input(PIN_A3)){}; //Wait switch
if(!input(PIN_A4)) //Poll SW3
{ delay_ms(10); //Debounce
if(time!=128)time=time*2; //Not if max
}
while(!input(PIN_A4)){}; //Wait switch
}
void forward() //4 steps clockwise /////////////
{
speed();
output_D(0x20); delay_ms(time); //Step 1
output_D(0x40); delay_ms(time); //Step 2
output_D(0x10); delay_ms(time); //Step 3
output_D(0x80); delay_ms(time); //Step 4
}
void reverse() //4 steps counter-clockwise /////
{
speed();
output_D(0x80); delay_ms(time); //Step 4
output_D(0x10); delay_ms(time); //Step 3
output_D(0x40); delay_ms(time); //Step 2
output_D(0x20); delay_ms(time); //Step 1
}
void main() //Main loop///////////////////////////////////////////
{
while(1) //Loop always
{
while(input(PIN_A2)) { forward(); } //Run forward
delay_ms(10); //Debounce
while(!input(PIN_A2)){}; //Wait until released
while(input(PIN_A2)) { reverse(); } //Run reverse
delay_ms(10); //Debounce
while(!input(PIN_A2)){}; //Wait until released
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 4.13 Light Switch
///////////////////////////////////////////////////////////////////////
// LIGHTCON.C
// Auto light switch uses comparator inputs on mechatronics board
// Pot 1 adjusted for light switching level.
// Connect: LIGHT to C1, POT1 to C1+
///////////////////////////////////////////////////////////////////////
#include "16F917.h"
void main()
{
setup_comparator(A0_A3_A1_A2); //Setup for PICDEM board
while(1)
{ if(!C1OUT) output_low(PIN_D7); //Switch off LED if light > pot
else output_high(PIN_D7); //Switch on LED if light < pot
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Listing 5.3 Temperature Controller Source Code
/*
TEMCON.C MPB 27-3-07
Temperature controller demo. Target simulation system: TEMCON.DSN
***********************************************************************/
#include " 16F877A.h "
#device ADC= 8 // 8-bit conversion
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1) // Display output
void main() //*****************************************************
{
float refin, numin, temp;
int1 flag;
setup_adc(ADC_CLOCK_INTERNAL); // Setup ADC
setup_adc_ports(ALL_ANALOG);
for(;;) // Repeat always
{
delay_ms(500);
set_adc_channel(0); // Read ref. volts
refin = read_adc();
set_adc_channel(1); // Read temp. volts
numin = read_adc();
temp = (numin*50)/256; // Calc. temperature
putc(254); putc(1); delay_ms(10);
printf( " Temp = %3.0 g " ,temp); // Display temp.
putc(254); putc(192); delay_ms(10);
if (numin < (refin-10)) // Temp. too low
{ output_high(PIN_B1); // Heater on
output_low(PIN_B2); // Fan off
flag = 1;
}
if (flag == 1) printf( " Heater ON " ); // Status message
if (numin > (refin + 10)) // Temp. too high
{ output_low(PIN_B1); // Heater off
output_high(PIN_B2); // Fan on
flag = 0;
}
if (flag == 0) printf( " Fan ON " ); // Status message
}
}
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE