Program Initial For CCS Command
Program Initial For CCS Command
}
}
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
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))
}
}
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
{
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
///////////////////////////////////////////////////////////////////////
#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