/Users/farahman/MPLABXProjects/MyFrist_C_Project.X/c_main.c
  1 /**********************************
  2  * Purpose:     The purpose of this project is to create a sample C project to 
  3  *              demonstrate basic features of XC8 compiler
  4  * Input:       None (no input is required)
  5  * Output:      PORTD 
  6  * Functions:   None
  7  * Device:      PIC18F45K20
  8  * Versions:    XC8 (V2.00) in free mode / MPLAB (V5.10)
  9  * Programmer:  PIC KIT 3
 10  * Project:     MyFirst C Project
 11  * File:        c_main.c
 12  * Author:      F. Farahmand
 13  * Created on December 29, 2018, 5:23 PM
 14  ***********************************/
 15 
 16 /** I N C L U D E S **************************************************/
 17 //#include "p18f46k20.h"
 18 
 19 #include "xc.h"
 20 #include "stdio.h"
 21 #include "math.h"
 22 #include "time.h"
 23 
 24 #include <xc.h>
 25 #include <stdint.h>
 26 #include <stdlib.h>
 27 
 28 
 29 /** C O N F I G U R A T I O N   B I T S ******************************/
 30 
 31 #pragma config FOSC = HS, FCMEN = OFF, IESO = OFF                            // CONFIG1H
 32 #pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                        // CONFIG2L
 33 #pragma config WDTEN = ON, WDTPS = 2048                                     // CONFIG2H
 34 #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
 35 #pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
 36 #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
 37 #pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
 38 #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
 39 #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
 40 #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
 41 #pragma config EBTRB = OFF   
 42 
 43 /** D E F I N I T I O N S  ******************************************/
 44 
 45 #define _XTAL_FREQ 8000000
 46 
 47 /*****************   EXAMPLE 1  ***********************/
 48 /** M A I N  ******************************************/
 49 
 50 void main (void)
 51 {
 52     int counter;
 53     char counter2;
 54     srand(1);
 55     counter = 1;
 56     TRISD = 0;
 57     
 58     while(counter <= 15)
 59     {
 60         PORTD = counter;
 61         counter++;
 62         PORTD = rand();
 63         counter = counter*2;
 64         printf("random %d \n", counter);
 65         __delay_ms(1000);
 66     }
 67 }
 68 
 69 /*****************   EXAMPLE 2  ***********************/
 70 /** D E C L A R A T I O N S **************************/
 71 // define other functions here. 
 72 
 73 // --- Example to set PORTB as output
 74 void main(void)
 75 {
 76     INTCON2bits.RBPU = 0;  //RB0 as Output PIN
 77     ADCON1= 0x7F; 
 78     WPUBbits.WPUB0 = 1;
 79     ANSELH = 0x00;
 80     counter = 1;
 81     TRISB = 0;
 82     
 83     while(counter <= 15)
 84     {
 85         PORTB = counter;
 86         counter++;
 87         __delay_ms(1000);
 88     }
 89 }
 90  
 91 /*****************   EXAMPLE 3  ***********************/
 92  --- setting port D
 93  int counter;
 94 void main(void)
 95 {
 96  
 97     counter = 1;
 98     TRISD = 0;
 99     
100     while(counter <= 15)
101     {
102         PORTD = counter;
103         counter++;
104         __delay_ms(1000);
105     }
106 }
107  
108 
109 /*****************   EXAMPLE 4  ***********************/
110  int main()
111 {
112     TRISB0 = 0; //RB0 as Output PIN
113 
114     while(1)
115     {
116         RB0 = 1;
117         __delay_ms(1000);
118         RB0 = 0;
119         __delay_ms(1000);
120     }
121     
122     return 0;
123 }
124 
125 
126 /*****************   EXAMPLE 5  ***********************/
127 // Program Memory
128 
129 unsigned char seg_code[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //Common Cathode
130 //unsigned char seg_code[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};//Common Anode
131 
132 void main()
133 {
134     TRISD = 0x00;
135     for (int i = 0; i<10; i++)
136     {
137         PORTD = seg_code[i];
138         __delay_ms(500);
139     }
140 }
141  
142 
143 /*****************   EXAMPLE 5  ***********************/
144 
145 void main (void) @0x100
146 {
147     while(1){
148         TRISD = 0x00;
149         TRISC = 0xFF; 
150         TRISE = 0xFF;
151 
152         asm("MOVLW 0x1F");
153         asm("ANDWF ANSEL");// RE0 & 1
154 
155         asm("MOVLW 0xFF");// each instruction takes 4 usec using Stopwatch
156         asm("MOVWF PORTD");// Set the simulator and see the signal
157         asm("MOVLW 0x0");
158         asm("MOVWF PORTD");// Measure the number of cycles that take to toggle
159         asm("MOVLW 0xFF"); // stop here
160         if (PORTEbits.RE0 ==1 ){
161             asm("MOVLW 0xA");
162             asm("MOVWF PORTD");// Measure teh number of cycles that take to toggle
163         }
164     }
165 
166 
167 
168     ADCON1 = 0x0F; // make ports pins digital
169     TRISB = 0x24; // make RB2 and RB5 inputs
170     // make RB4 and output
171     PORTB = 0x00; // alarm off
172     INTCON2bits.RBPU = 1; // Port B pullups on
173     RCONbits.IPEN = 1; // IPEN = 1
174     INTCON2bits.INTEDG2 = 1; // make INT2 positive edge-trig
175     INTCON3bits.INT2IP = 0; // make INT2 low priority
176     INTCON3bits.INT2IE = 1; // enable INT2
177     INTCONbits.GIEH = 1; // enable high priority interrupts
178     INTCONbits.GIEL = 1; // enable low priority interrupts
179     while( 1 ) // main program loop
180     {
181         ClrWdt(); // pet spot (woof/pant)
182         if ( PORTBbits.RB5 == 0 ) // pushbutton pressed
183         PORTBbits.RB4 = 0; // alarm off
184     }
185 }
186