Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #define RCC_AHBENR (*(unsigned int *)(0x40021000 + 0x14))
  2. #define GPIOB_MODER (*(unsigned int *)(0x48000400 + 0x00))
  3. #define GPIOB_ODR (*(unsigned int *)(0x48000400 + 0x14))
  4. #define RCC_APB1ENR (*(unsigned int *)(0x40021000 + 0x1C))
  5. #define PWR_CR (*(unsigned int *)(0x40007000 + 0x00))
  6. #define RCC_BDCR (*(unsigned int *)(0x40021000 + 0x20))
  7. #define RTC_WPR (*(unsigned int *)(0x40002800 + 0x24))
  8. #define RTC_ISR (*(unsigned int *)(0x40002800 + 0x0C))
  9. #define RTC_DR (*(unsigned int *)(0x40002800 + 0x04))
  10. #define RTC_TR (*(unsigned int *)(0x40002800 + 0x00))
  11.  
  12. #define GPIOA_PUPDR (*(unsigned int *)(0x48000000 + 0x0C))
  13. #define GPIOA_IDR (*(unsigned int *)(0x48000000 + 0x10))
  14. #define GPIOA_ODR (*(unsigned int *)(0x48000000 + 0x14))
  15. #define GPIOA_MODER (*(unsigned int *)(0x48000000 + 0x00))
  16.  
  17. int test = 0;
  18. int token = 0;
  19. int counter = 0;
  20. int i;
  21.  
  22. void clockSetup() {
  23. RCC_APB1ENR = (1 << 28);
  24. PWR_CR = (1 << 8);
  25. RCC_BDCR = 0x8101;
  26. RTC_WPR = 0xCA;
  27. RTC_WPR = 0x53;
  28. RTC_ISR = (1 << 7);
  29. while ((RTC_ISR & 0x40) == 0) {
  30. asm("nop");
  31. }
  32. RTC_DR = 0x0;
  33. RTC_TR = 0x0;
  34. RTC_ISR &= (0 << 7);
  35. RTC_WPR = 0xFE;
  36. RTC_WPR = 0x64;
  37. PWR_CR &= (0 << 8);
  38. }
  39.  
  40. void boardSetup() {
  41. RCC_AHBENR |= (1 << 17);
  42. GPIOA_PUPDR |= 0x4;
  43. GPIOA_MODER |= 0x400;
  44. GPIOA_MODER |= 0x4000;
  45. }
  46.  
  47. void clock(int pulse) {
  48. for (int k = 0; k < pulse; k++) {
  49. GPIOA_ODR |= 0x20;
  50. GPIOA_ODR &= ~0x20;
  51. }
  52. }
  53.  
  54. void begin() {
  55. GPIOA_ODR |= 0x00A0;
  56. }
  57.  
  58. void start() {
  59. GPIOA_ODR &= ~0x80;
  60. GPIOA_ODR &= ~0x20;
  61. }
  62.  
  63. void stop() {
  64. GPIOA_ODR |= 0x20;
  65. GPIOA_ODR |= 0x80;
  66. }
  67.  
  68. void acknowledge() {
  69. clock(1);
  70. }
  71.  
  72. void zero() {
  73. GPIOA_ODR |= 0x80;
  74. clock(6);
  75. GPIOA_ODR &= ~0x80;
  76. clock(1);
  77. GPIOA_ODR |= 0x80;
  78. clock(1);
  79. GPIOA_ODR &= ~0x80;
  80. acknowledge();
  81. }
  82.  
  83. void one() {
  84. clock(1);
  85. GPIOA_ODR |= 0x80;
  86. clock(2);
  87. GPIOA_ODR &= ~0x80;
  88. clock(4);
  89. GPIOA_ODR |= 0x80;
  90. clock(1);
  91. GPIOA_ODR &= ~0x80;
  92. acknowledge();
  93. }
  94.  
  95. void two() {
  96. GPIOA_ODR |= 0x80;
  97. clock(2);
  98. GPIOA_ODR &= ~0x80;
  99. clock(1);
  100. GPIOA_ODR |= 0x80;
  101. clock(2);
  102. GPIOA_ODR &= ~0x80;
  103. clock(1);
  104. GPIOA_ODR |= 0x80;
  105. clock(2);
  106. GPIOA_ODR &= ~0x80;
  107. acknowledge();
  108. }
  109.  
  110. void three() {
  111. GPIOA_ODR |= 0x80;
  112. clock(4);
  113. GPIOA_ODR &= ~0x80;
  114. clock(2);
  115. GPIOA_ODR |= 0x80;
  116. clock(2);
  117. GPIOA_ODR &= ~0x80;
  118. acknowledge();
  119. }
  120.  
  121. void four() {
  122. clock(1);
  123. GPIOA_ODR |= 0x80;
  124. clock(2);
  125. GPIOA_ODR &= ~0x80;
  126. clock(2);
  127. GPIOA_ODR |= 0x80;
  128. clock(3);
  129. GPIOA_ODR &= ~0x80;
  130. acknowledge();
  131.  
  132. }
  133.  
  134. void five() {
  135. GPIOA_ODR |= 0x80;
  136. clock(1);
  137. GPIOA_ODR &= ~0x80;
  138. clock(1);
  139. GPIOA_ODR |= 0x80;
  140. clock(2);
  141. GPIOA_ODR &= ~0x80;
  142. clock(1);
  143. GPIOA_ODR |= 0x80;
  144. clock(3);
  145. GPIOA_ODR &= ~0x80;
  146. acknowledge();
  147. }
  148.  
  149. void six() {
  150. GPIOA_ODR |= 0x80;
  151. clock(1);
  152. GPIOA_ODR &= ~0x80;
  153. clock(1);
  154. GPIOA_ODR |= 0x80;
  155. clock(6);
  156. GPIOA_ODR &= ~0x80;
  157. acknowledge();
  158. }
  159.  
  160. void seven() {
  161. GPIOA_ODR |= 0x80;
  162. clock(3);
  163. GPIOA_ODR &= ~0x80;
  164. clock(4);
  165. GPIOA_ODR |= 0x80;
  166. clock(1);
  167. GPIOA_ODR &= ~0x80;
  168. acknowledge();
  169. }
  170.  
  171. void eight() {
  172. GPIOA_ODR |= 0x80;
  173. clock(8);
  174. GPIOA_ODR &= ~0x80;
  175. acknowledge();
  176. }
  177.  
  178. void nine() {
  179. GPIOA_ODR |= 0x80;
  180. clock(4);
  181. GPIOA_ODR &= ~0x80;
  182. clock(1);
  183. GPIOA_ODR |= 0x80;
  184. clock(3);
  185. GPIOA_ODR &= ~0x80;
  186. acknowledge();
  187.  
  188. }
  189. void dataCommand() {
  190. start();
  191. clock(6);
  192. GPIOA_ODR |= 0x80;
  193. clock(1);
  194. GPIOA_ODR &= ~0x80;
  195. clock(1);
  196. acknowledge();
  197. stop();
  198. }
  199.  
  200. void displayControllCommand() {
  201. start();
  202. clock(2);
  203. GPIOA_ODR |= 0x0080;
  204. clock(2);
  205. GPIOA_ODR &= ~0x0080;
  206. clock(3);
  207. GPIOA_ODR |= 0x0080;
  208. clock(1);
  209. GPIOA_ODR &= ~0x0080;
  210. acknowledge();
  211. stop();
  212. }
  213.  
  214. void addressCommand() {
  215. start();
  216. clock(6);
  217. GPIOA_ODR |= 0x80;
  218. clock(2);
  219. GPIOA_ODR &= ~0x0080;
  220. acknowledge();
  221. }
  222. void display(int sec) {
  223. addressCommand();
  224. switch (sec) {
  225. case 0:
  226. zero();
  227. break;
  228. case 1:
  229. one();
  230. break;
  231. case 2:
  232. two();
  233. break;
  234. case 3:
  235. three();
  236. break;
  237. case 4:
  238. four();
  239. break;
  240. case 5:
  241. five();
  242. break;
  243. case 6:
  244. six();
  245. break;
  246. case 7:
  247. seven();
  248. break;
  249. case 8:
  250. eight();
  251. break;
  252. case 9:
  253. nine();
  254. break;
  255. }
  256.  
  257. stop();
  258. }
  259.  
  260. int timerDelay(int sec) {
  261. if ((RTC_TR & 0x1) == 0) {
  262. if (test == 0) {
  263. sec++;
  264. test++;
  265. }
  266.  
  267. }
  268. if ((RTC_TR & 0x1) == 1) {
  269. if (test == 1) {
  270. sec++;
  271. test--;
  272. }
  273.  
  274. }
  275. return sec;
  276. }
  277.  
  278. void reset(int sec) {
  279. sec = 0;
  280. display(sec);
  281. }
  282.  
  283. int main() {
  284. int sec = 0;
  285. boardSetup();
  286. begin();
  287. dataCommand();
  288. displayControllCommand();
  289. clockSetup();
  290. reset(sec);
  291. token = ~token;
  292. while (1) {
  293. display(sec % 10);
  294. display(sec / 10 % 6);
  295. display(sec / 60 % 10);
  296. display(sec / 600 % 10);
  297. sec = timerDelay(sec);
  298.  
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement