1 | program Flash_Test
|
2 | 'Used PIC is a PIC12F1572
|
3 | 'INTOSC is set to 32Mhz
|
4 | '
|
5 | 'Program writes dummys to the HEF Flashcell and give it out over UART.
|
6 | 'After this, program fall into a endless loop.
|
7 | '
|
8 | '
|
9 |
|
10 | Const Row1 as Word = 0x780
|
11 | Const Row2 as Word = 0x7A0
|
12 | Const Row3 as Word = 0x7C0
|
13 | Const Row4 as Word = 0x7E0
|
14 |
|
15 | dim UART_Data_Out as char[5]
|
16 |
|
17 | dim MyMemory as word[16]
|
18 | dim i as byte
|
19 |
|
20 | Sub Procedure Init
|
21 | ANSELA = 0 'All Pins are digital
|
22 | WPUA = 0 'No Pullups
|
23 | INLVLA = 0x0 'All Inputs are TTL
|
24 | OSCCON = 0xF6 'Configure 32Mhz internal OSC
|
25 | UART1_Init(9600) 'Init Uart
|
26 | Delay_ms(100) 'Wait until Uart stable
|
27 | End sub
|
28 |
|
29 | sub procedure Flash_Unlock
|
30 | PMCON2 = 0x55
|
31 | PMCON2 = 0xAA
|
32 | PMCON1.WR = 1
|
33 | NOP
|
34 | NOP
|
35 | end sub
|
36 |
|
37 | sub procedure FlashEraseRow (dim Address as word)
|
38 | INTCON.GIE = 0 'disable all interrupts
|
39 | PMCON1.CFGS = 0 'set program memory space
|
40 | PMCON1.PMADR = Address
|
41 | PMCON1.FREE = 1
|
42 | PMCON1.WREN = 1
|
43 | Flash_unlock
|
44 | 'processor stalls until operation is complete (2 mS typical)
|
45 | PMCON1.WREN = 0
|
46 | INTCON.GIE = 1 'restore old value of GIE bit
|
47 | end sub
|
48 |
|
49 | sub procedure FlashWriteRow(dim Address as word, dim byref datas as word[16])
|
50 | dim i as byte
|
51 | INTCON.GIE = 0 'disable all interrupts
|
52 | PMCON1.CFGS = 0 'set program memory space
|
53 | PMCON1.PMADR = Address
|
54 | PMCON1.FREE = 0
|
55 | PMCON1.LWLO = 1 'only write to the latches
|
56 | PMCON1.WREN = 1
|
57 | for i =0 to 15
|
58 | PMCON1.PMADR = Address + i
|
59 | PMCON1.PMDAT = datas[i]
|
60 | Flash_unlock
|
61 | next i
|
62 | PMCON1.LWLO = 0 'write latches to flash
|
63 | Flash_unlock
|
64 | 'processor stalls until operation is complete (2 mS typical)
|
65 | PMCON1.WREN = 0
|
66 | INTCON.GIE = 1 'restore old value of GIE bit
|
67 | end sub
|
68 |
|
69 | sub function FlashReadWord (dim Address as word) as word
|
70 | PMCON1.CFGS = 0 'set program memory space
|
71 | PMCON1.PMADRH = Address
|
72 | PMCON1.RD = 1 'set read
|
73 | NOP
|
74 | NOP
|
75 | FlashReadWord = PMCON1.PMDAT
|
76 | end sub
|
77 |
|
78 | sub procedure FlashReadRow (dim Address as word,dim byref datas as word[16])
|
79 | dim i as byte
|
80 | for i =0 to 15
|
81 | datas[i] = FlashReadWord(Address +i)
|
82 | next i
|
83 | end sub
|
84 |
|
85 | Sub Procedure WriteDummysToFlash
|
86 | for i = 0 to 15
|
87 | MyMemory[i] = i
|
88 | next i
|
89 | FlashWriteRow(Row1,MyMemory)
|
90 | for i = 0 to 15
|
91 | MyMemory[i] = i +16
|
92 | next i
|
93 | FlashWriteRow(Row2,MyMemory)
|
94 | for i = 0 to 15
|
95 | MyMemory[i] = i +32
|
96 | next i
|
97 | FlashWriteRow(Row3,MyMemory)
|
98 | for i = 0 to 15
|
99 | MyMemory[i] = i +48
|
100 | next i
|
101 | FlashWriteRow(Row4,MyMemory)
|
102 | end sub
|
103 |
|
104 | Sub Procedure SendMyMemoryToPC
|
105 | for i = 0 to 15
|
106 | WordToStr(MyMemory[i],UART_Data_Out)
|
107 | UART1_Write_Text(UART_Data_Out)
|
108 | next i
|
109 | end sub
|
110 |
|
111 | main:
|
112 | Init
|
113 | WriteDummysToFlash
|
114 | FlashReadRow(Row1,MyMemory)
|
115 | SendMyMemoryToPC
|
116 | FlashReadRow(Row2,MyMemory)
|
117 | SendMyMemoryToPC
|
118 | FlashReadRow(Row3,MyMemory)
|
119 | SendMyMemoryToPC
|
120 | FlashReadRow(Row4,MyMemory)
|
121 | SendMyMemoryToPC
|
122 |
|
123 | while 1
|
124 | clrwdt
|
125 | wend
|
126 |
|
127 | end.
|