In this paper, using the potential of microgrid surface batteries, the frequency support of the power system in different operating modes was discussed. The study suggested that the process could be supported by a set of batteries in the microgrid (in the mains-connected mode and in the island mode, with the decentralized participation of each, in frequency support (power system or microgrid) in Different operating modes, were addressed. The approach used in this paper was to use a convex optimization algorithm. In this way, the batteries independently measured the system frequency and considering their charging mode injected the optimal power into the system, and A convex optimization algorithm will prove that the local optimal point (scattered battery injection) is the optimal point. It will also be universal (optimal system frequency control). Studies in system simulation in two modes of microgrid connected to the main grid and island microgrid have yielded several points.