PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
devid.c
1 #include "p24Hxxxx.h"
2 #include <stdio.h>
3 #include "pic24_uart.h"
4 #include "pic24_delay.h"
5 
6 
7 /** Notes
8  * - The end of the p24HJ12GP201.h include file in
9  * C:\Program Files\Microchip\MPLAB C30\support\h
10  * contains lots of useful stuff:
11  * - It defines bit names for all uniquely-named bits
12  * in the chip.
13  * For example, from line 1828 of v3.01 of the file:
14  * #define _C SRbits.C
15  * - Useful macros are defined starting on line 2741 of the
16  * file: Nop(), ClrWdt(), and so on.
17  * - Macros for delcaring ISRs at line 2798:
18  * _ISR and _ISRFAST. Using the correct name for the
19  * interrupt (see comments near that line) cause it to
20  * be placed at the appropriate place in the interrupt
21  * table.
22  * - Config bit setting begin at line 2843. Instead of
23  * ORing bits, AND them. To quote the
24  * comments there:
25  * ** Only one invocation of (each config register) should appear in a project,
26  * ** at the top of a C source file (outside of any function).
27  */
28 
29 
30 // Define if using the simulator
31 //#define SIM
32 
33 #include "pic24_clockfreq.h"
34 #include "pic24_configbits.h"
35 #include "pic24_delay.h"
36 #include "pic24_uart.h"
37 #include "pic24_ports.h"
38 
39 typedef short Word16;
40 typedef unsigned short UWord16;
41 typedef long Word32;
42 typedef unsigned long UWord32;
43 typedef union tuReg32 {
44  UWord32 Val32;
45 
46  struct {
47  UWord16 LW;
48  UWord16 HW;
49  } Word;
50 
51  char Val[4];
52 } uReg32;
53 
54 extern UWord32 ReadLatch(UWord16, UWord16);
55 /*
56 IO configuration Macros go here
57 */
58 
59 //_PCFG2 = 1;// Use RB0/AN2 for digital I/O
60 //_TRISB0 = 0;// Define RB0 as a digital output
61 //port Macros defined in pic24_ports.h
62 #define CONFIG_LED1 CONFIG_RB13_AS_DIG_OUTPUT
63 
64 #define LED1 _LATB13
65 //Prints out the Device ID, ProcessID of the processor
66 int main (void) {
67  unsigned char c;
68  configClock(); //defined in pic24_clockfreq.c
69 
70  /************************* UART config ********************/
71  //UART PIN config macros defined in "pic24_uart.h"
72  CONFIG_UART_TX_TO_RP15;
73  CONFIG_UART_RX_TO_RP14;
74  //CONFIG_UART_TX_TO_RP0;
75  //CONFIG_UART_RX_TO_RP1;
76  CONFIG_UART_BAUDRATE(38400);
77  // 2. Set the number of data bits, number of Stop bits and
78  // parity selection by writing to the PDSEL<1:0>
79  // (UxMODE<2:1>) and STSEL (UxMODE<0>) bits.
80  CONFIG_UART_8DATA_NOPARITY; // 8-bit data, no parity
81  CONFIG_UART_ONE_STOPBIT; // 1 Stop bit
82 
83  ENABLE_UART;
84 
85  /*************************** GPIO config ***************************/
86  CONFIG_LED1;
87  puts("Hello from Devid!\n\r");
88  {
89  uReg32 SourceAddr;
90  uReg32 Temp;
91  UWord16 devid;
92  UWord16 processid;
93 
94  SourceAddr.Val32 = 0xFF0000;
95 
96  Temp.Val32 = ReadLatch(SourceAddr.Word.HW, SourceAddr.Word.LW);
97  devid = ((Temp.Val[1] << 8) &0xFF00)|(Temp.Val[0]);
98  printf("Device ID: %x\n\r",devid);
99 
100  SourceAddr.Val32 = 0xFF0002;
101 
102  Temp.Val32 = ReadLatch(SourceAddr.Word.HW, SourceAddr.Word.LW);
103 
104  processid = (Temp.Val[1]>>4)&0x0F;
105  printf("Process ID: %x\n\r",processid);
106  }
107  /************************* Echo code ******************************/
108  // Echo character + 1
109  // Toggle LED with each character received.
110  while (1);
111 
112  return 0;
113 }