GenProcs.c


1
/*********************************************************************
2
 *              General Procedures Libary Source Code
3
 *********************************************************************
4
 * FileName:        GenProcs.c
5
 * Dependencies:    GenProcs.h
6
 * Processor:       PIC18F4685
7
 * Complier:        Microchip C18
8
 *
9
 *
10
 * Author               Date    Comment
11
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12
 * Stephan Krause       17/8/12 Version 1.0 - Initial Release
13
 *********************************************************************/
14
15
16
17
#include "GenProcs.h"
18
19
20
21
22
23
////////////////////////////////
24
//  Initialisierung des PICs
25
///////////////////////////////
26
void Init(void){
27
  unsigned int i;
28
  
29
  InitOSC();  
30
  
31
  InitPORTA();  
32
  InitPORTB();  
33
  InitPORTC();  
34
  InitPORTD();  
35
  InitPORTE();  
36
  
37
  LED_RT = 1;
38
  LED_GR = 1;
39
40
  
41
  InitTimer();  
42
  InitPWM1();
43
  InitInterrupts();
44
  InitADC(2);
45
  LCD_Init(ON);
46
  
47
  // Configure the device to access Data EEPROM memory
48
  EECON1bits.EEPGD = 0;
49
  EECON1bits.CFGS = 0;
50
  
51
  // laden der Fahrregler nummer aus dem internen eeprom
52
  //FR_ID = (unsigned int)ReadEEPROM((unsigned int)FR_ID_ADRL) + (((unsigned int)ReadEEPROM((unsigned int)FR_ID_ADRH))<<8);
53
  
54
  
55
  //OpenI2C(MASTER, SLEW_ON);    // Initialize I2C module
56
  //SSPADD = 19;          //400kHz Baud clock(19) @32MHz
57
                  //100kHz Baud clock(79) @32MHz
58
  
59
}
60
61
62
////////////////////////////////
63
//  Initialisieren von PORTA
64
////////////////////////////////
65
void InitPORTA(void){
66
  PORTA=0x00;            // Clear 
67
  TRISA=0b11110111;        // initialize Port A: 0,1,2,3 as Input, rest output
68
}
69
70
////////////////////////////////
71
//  Initialisieren von PORTB
72
////////////////////////////////
73
void InitPORTB(void){
74
  PORTB=0x00;            // Clear
75
  TRISB=0b11101101;        // 1: Input, 0: Output
76
  
77
}
78
////////////////////////////////
79
//  Initialisieren von PORTC
80
////////////////////////////////
81
void InitPORTC(void){
82
  PORTC=0x00;            // clear
83
  TRISC=0b11111011;        // all Input; RC6 output for TX1uc
84
}
85
////////////////////////////////
86
//  Initialisieren von PORTD
87
////////////////////////////////
88
void InitPORTD(void){
89
  PORTD=0x00;            // clear
90
  TRISD=0x00;            // all Output for lcd data lines
91
}
92
////////////////////////////////
93
//  Initialisieren von PORTE
94
////////////////////////////////
95
void InitPORTE(void){
96
  PORTE=0x00;            // clear
97
  TRISE=0x00;            // all out for lcd en,rw,rs
98
}
99
100
101
102
void InitTimer(void){  
103
  OpenTimer0(  TIMER_INT_ON &
104
        T0_8BIT &
105
        T0_SOURCE_INT &
106
        T0_PS_1_128 );
107
  WriteTimer0(101);
108
}  
109
110
111
void InitOSC(void){
112
  OSCCON |= 0b01110000;
113
  OSCTUNE |= 0b01000000;
114
  Delay10KTCYx(1);
115
}  
116
117
118
119
//==================================
120
// EEPROM read routine
121
122
unsigned char ReadIntEEPROM(unsigned int Address){
123
  EEADRH = (unsigned char)(Address>>8); // Load the high byte of the EEPROM address
124
  EEADR = (unsigned char)Address; // Load the low byte of the EEPROM address
125
  EECON1bits.EEPGD=0;
126
  EECON1bits.RD = 1; // Do the read
127
  return EEDATA; // Return with the data
128
}
129
130
//==================================
131
// EEPROM write routine
132
133
void WriteIntEEPROM(unsigned int Address, unsigned char Data){
134
  static unsigned char GIE_Status; // Variable to save Global Interrupt Enable bit
135
136
  EEADRH = (unsigned char)(Address>>8);   // Load the high byte of the EEPROM address
137
  EEADR = (unsigned char)Address;     // Load the low byte of the EEPROM address
138
  EEDATA = Data;               // Load the EEPROM data
139
  EECON1bits.WREN = 1;           // Enable EEPROM writes
140
  GIE_Status = INTCONbits.GIE;       // Save the Global Interrupt Enable bit
141
  INTCONbits.GIE = 0;           // Disable global interrupts
142
  EECON2 = 0x55;               // Required sequence to start the write cycle
143
  EECON2 = 0xAA;               // Required sequence to start the write cycle
144
  EECON1bits.WR = 1;             // Required sequence to start the write cycle
145
  INTCONbits.GIE = GIE_Status;       // Restore the Global Interrupt Enable bit
146
  EECON1bits.WREN = 0;           // Disable EEPROM writes
147
  while (EECON1bits.WR);           // Wait for the write cycle to complete
148
}
149
150
151
152
int GetEnc_Delta(char clear){
153
  int buffer;
154
  INTCONbits.GIE = 0;    // disable global interrupts  
155
  buffer = enc_delta;
156
  if(clear) enc_delta = 0;
157
  INTCONbits.GIE = 1;    // enable global interrupts  
158
  return buffer;
159
}